Search the Community
Showing results for tags 'foreach arrays'.
-
I'm still pretty new to php and have an issue i'm trying to solve, I have the following two arrays The first one is contains product id's and the quantity ordered Array ( [35659] => 1 [35699] => 1 [35735] => 2 ) The second one contains warehouse locations that stock the product and the quantity they have available Array ( [35659] => Array ( [9] => 10 [114] => 1 [126] => 0 ) [35699] => Array ( [9] => 8 [114] => 0 [126] => 5 ) [35735] => Array ( [9] => 10 [114] => 0 [126] => 0 ) ) So what I am trying to do is loop through and add to an array each warehouse that meets the quantity of the order here is my code: $stockrequired = array('35659' => '1', '35699' => '1', '35735' => '2'); $instock = array(); foreach ($locations as $location) { foreach($stockrequired as $id => $qty){ if($stockavailable[$id][$location] >= $qty){ $instock[$id] = $location; } } } print_r($instock); However this produces the following which is the last warehouse that meets the quantity. Array ( [35659] => 114 [35699] => 126 [35735] => 9 ) What I need is all the warehouses the meet the quantity for each product e.g. my desired outcome will be below. I'm guessing my loop is getting reset or something? Any help would be appreciated. Array ( [35659] => 9 [35659] => 114 [35659] => 9 [35699] => 126 [35735] => 9 )