I have this array:
0 => array:3 [
\"product_id\" => \"1138\"
\"product_image\" => \"/resources/medias/shop/products/shop-6500720--1.png\"
I refactored the elegant approach from @Chayan into a function so it can be used like array_column()
. Keys to be filtered can now be presented as a simple array.
Btw this is most likely also the fastest approach, since it uses build-in functions for most of the heavy lifting.
"1138",
"product_image" => "/resources/medias/shop/products/shop-6500720--1.png",
"product_sku" => "6500722"
],
[
"product_id" => "1144",
"product_image" => "/resources/medias/shop/products/shop-6501041--1.png",
"product_sku" => "6501046"
],
[
"product_id" => "113",
"product_image" => "/resources/medias/shop/products/shop-6294909--1.png",
"product_sku" => "6294915"
]
);
$keysSelect = array("product_id" , "product_sku");
$filteredArray = array_colums($arr, $keysSelect);
var_dump($filteredArray);