ajhanna88 Posted July 12, 2014 Share Posted July 12, 2014 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 ) Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted July 12, 2014 Share Posted July 12, 2014 to make an array of arrays, change the $instock[$id] = $location; line to the following - $instock[$id][] = $location; Quote Link to comment Share on other sites More sharing options...
ajhanna88 Posted July 12, 2014 Author Share Posted July 12, 2014 awesome that worked thanks. I now want to group this array: Array ( [35659] => 9 [35659] => 114 [35659] => 9 [35699] => 126 [35735] => 9 ) into Array ( [9] => Array ( [35659] [35659] [35735] ) [114] = > Array ( [35659] ) [126] = > Array ( [35699] ) ) e.g. group by warehouse that contains the most products? Thanks Quote Link to comment Share on other sites More sharing options...
adam_bray Posted July 12, 2014 Share Posted July 12, 2014 It's similar to above, use a foreach loop and add arrays inside arrays - $warehouses = array(); foreach($array as $product => $warehouse) { $warehouses[$warehouse] = $product; } print_r($warehouses); Quote Link to comment Share on other sites More sharing options...
ajhanna88 Posted July 13, 2014 Author Share Posted July 13, 2014 (edited) It's similar to above, use a foreach loop and add arrays inside arrays - $warehouses = array(); foreach($array as $product => $warehouse) { $warehouses[$warehouse] = $product; } print_r($warehouses); Thanks, that worked the final part of my puzzle is now compare the below array product ids Array ( [35659] => 1 [35699] => 1 [35735] => 2 ) to this one, I want to loop through until each product id has been matched in the second array. Array ( [9] => Array ( [35659] => 35659 [35699] => 35699 [35735] => 35735 ) [126] => Array ( [35659] => 35659 [35699] => 35699 ) [114] => Array ( [35659] => 35659 ) ) what i am trying to produce is just this [9] => Array ( [35659] => 35659 [35699] => 35699 [35735] => 35735 ) Thanks Edited July 13, 2014 by ajhanna88 Quote Link to comment Share on other sites More sharing options...
adam_bray Posted July 13, 2014 Share Posted July 13, 2014 Post where you get stuck and I'm sure someone can point you in the right direction. You need to at least try something before asking for help. Quote Link to comment Share on other sites More sharing options...
ajhanna88 Posted July 13, 2014 Author Share Posted July 13, 2014 (edited) Post where you get stuck and I'm sure someone can point you in the right direction. You need to at least try something before asking for help. I agree, I have made an attempt however it's not a great one. i don't expect to be given the answer just some advice to get me going on the right lines. Thanks //loop through each product in order foreach ($stockrequired as $id) { //loop through all items in stock foreach ($instock as $key => $value) { //here I want to match the "product ids that are the same in both arrays while ($id == $value) { //while stock required id is equal to instock id } } } Edited July 13, 2014 by ajhanna88 Quote Link to comment Share on other sites More sharing options...
adam_bray Posted July 13, 2014 Share Posted July 13, 2014 Have you looked at in_array and array_key_exists? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.