soyaslim Posted August 17, 2022 Share Posted August 17, 2022 Hello guys, I have this array that I want the 'distance' key value to another array if this array 'supplier' matches the other array 'supplier' value. Array ( [0] => Array ( [supplier] => 'ROLAN' [distance] => 3.8 ) [1] => Array ( [supplier] => 'COOLDRIVE' [distance] => 6.2 ) [2] => Array ( [supplier] => 'ENGINEERING.' [distance] => 14.6 ) [3] => Array ( [supplier] => 'TEST DEPO' [distance] => 9.0 ) ) The other array is So, I just want to add 'distance' from the top array to below which I did already with the foreach loop. Array ( [0] => Array ( [supplier] => ENGINEERING [rolanID] => Array ( [0] => 51154 ) ) [1] => Array ( [supplier] => COOLDRIVE [rolanID] => Array ( [0] => 2338117 ) ) [2] => Array ( [supplier] => ROLAN [rolanID] => Array ( [0] => 2338117 [1] => 51154 ) ) ) So, Is there any best way than this below my code: Thank you foreach ($results as $row) { // this is second array loop $supplierAvailable[$row['supplier']]['supplier'] = $row['supplier']; $supplierAvailable[$row['supplier']]['rolanID'][] = $row['rolan_part_id']; foreach ($supplierDetails as $index => $depo) { // this is the first array loop to add distance to second array if ($depo['supplier'] == "'".$row['supplier']."'") { $supplierAvailable[$row['supplier']]['distance'] = $depo['distance']; unset($depo[$index]); break; } } } Quote Link to comment https://forums.phpfreaks.com/topic/315196-best-way-to-manipulate-multi-dimensional-array/ Share on other sites More sharing options...
kicken Posted August 18, 2022 Share Posted August 18, 2022 (edited) Turn your first array into a map, so it would look like this: Array ( [ROLAN] => 3.8 [COOLDRIVE] => 6.2 [ENGINEERING.] => 14.6 [TEST DEPO] => 9 ) You can use a loop or array_column to accomplish that. Then you can simply look up the supplier in that map by accessing the appropriate index rather than having to loop and search for it. $supplierAvailable[$row['supplier']]['distance'] = $distanceMap[$row['supplier']] Edited August 18, 2022 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/315196-best-way-to-manipulate-multi-dimensional-array/#findComment-1599503 Share on other sites More sharing options...
soyaslim Posted August 19, 2022 Author Share Posted August 19, 2022 On 8/18/2022 at 10:27 AM, kicken said: Turn your first array into a map, so it would look like this: Array ( [ROLAN] => 3.8 [COOLDRIVE] => 6.2 [ENGINEERING.] => 14.6 [TEST DEPO] => 9 ) You can use a loop or array_column to accomplish that. Then you can simply look up the supplier in that map by accessing the appropriate index rather than having to loop and search for it. $supplierAvailable[$row['supplier']]['distance'] = $distanceMap[$row['supplier']] Hey Kicken, actually the array 'supplier' value has a single quote like below Array ( [0] => Array ( [supplier] => 'ROLAN' [distance] => 3.8 ) so, the it does not match the following code as ROLAN is not equal to 'ROLAN' $supplierAvailable[$row['supplier']]['distance'] = $distanceMap[$row['supplier']] Quote Link to comment https://forums.phpfreaks.com/topic/315196-best-way-to-manipulate-multi-dimensional-array/#findComment-1599573 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.