I have this multidimensional array that I need to find how many items for any SKU are sold.
How I can do?
[Orders] => Array(
[0] => Array(
[transaction_id] => xxxx
[date] => 1616277653
[currency] => EUR
[items] => Array(
[0] => Array(
[title] => Title
[quantity] => 200
[price] => 2.1
[sku] => 1T.810BK
)
[1] => Array(
[title] => Title
[quantity] => 250
[price] => 1.78
[sku] => 1090.005BK
)
)
)
[1] => Array(
[transaction_id] => 78079
[date] => 1616597838
[currency] => EUR
[items] => Array(
[0] => Array
(
[title] => title
[quantity] => 3
[price] => 118.3
[sku] => 1.DIYBOX.ULT.638RE
)
)
)
)
For the moment I was using a function with array_walk_recursive
inside, to extract all the values of a Key SKU.
But in this way I cannot associate it with the value of the quantity.
So this can't be the solution, unless I can't pass 2 Key variables inside the function. It can be done?
This is the function that I use at this moment:
function array_value_recursive($key, array $arr){
$val = array();
array_walk_recursive($arr, function($v, $k) use($key, &$val){
if($k == $key) array_push($val, $v);
});
return count($val) > 1 ? $val : array_pop($val);
}
array_value_recursive('sku', $GetPendingOrders_obj);
I'm not sure if passing two keys is the ideal solution. How do you think I can solve the problem of finding the quantity of the SKU sold?