soyaslim Posted August 11, 2022 Share Posted August 11, 2022 (edited) Hello guys. I want the array to be processed into my final array how am I supposed to achieve this. Below is the array I want to process. Array ( Array ( [part_id] => 2338117 [supplier] => COOLDRIVE DISTRIBUTION [quantity] => 12 ) Array ( [part_id] => 2338117 [supplier] => ROLAN [quantity] => 20 ) Array ( [part_id] => 51154 [supplier] => ROLAN [quantity] => 20 ) ) This is the final array I want. Array ( [COOLDRIVE DISTRIBUTION] => Array ( [proudctID] => Array ( [0] => 2338117 ) ) [ROLAN] => Array ( [productID] => Array ( [0] => 2338117 [1] => 51154 ) ) ) Edited August 11, 2022 by soyaslim Quote Link to comment https://forums.phpfreaks.com/topic/315167-how-to-process-the-multi-dimensional-array-into-specific-array/ Share on other sites More sharing options...
Barand Posted August 11, 2022 Share Posted August 11, 2022 What have you tried so far? Quote Link to comment https://forums.phpfreaks.com/topic/315167-how-to-process-the-multi-dimensional-array-into-specific-array/#findComment-1599241 Share on other sites More sharing options...
soyaslim Posted August 11, 2022 Author Share Posted August 11, 2022 for ($i=0; $i<count($results)-1; $i++) { $productID = ($results[0]['supplier'] == $results[$i+1]['supplier']) ? array($results[$i]['rolan_part_id'], $results[$i+1]['rolan_part_id']) : array($results[$i+1]['rolan_part_id']); $finalArray[$results[$i]['supplier']] = array( "productID" => $productID, ); } print_r($finalArray); so, the $results contain the array I want to change, which is three nested arrays. The above output is: Array ( [COOLDRIVE DISTRIBUTION] => Array ( [productID] => Array ( [0] => 2338117 ) ) [ROLAN] => Array ( [productID] => Array ( [0] => 51154 ) ) ) I couldn't get the other productID in 'ROLAN' array. Quote Link to comment https://forums.phpfreaks.com/topic/315167-how-to-process-the-multi-dimensional-array-into-specific-array/#findComment-1599242 Share on other sites More sharing options...
Solution Barand Posted August 11, 2022 Solution Share Posted August 11, 2022 All you need to do is loop through your original array using foreach() and append the products to a new array, providing the required key values. foreach ($orig as $rec) $new[ $rec['supplier'] ]['productID'][] = $rec['part_id']; Result for $new Array ( [COOLDRIVE DISTRIBUTION] => Array ( [productID] => Array ( [0] => 2338117 ) ) [ROLAN] => Array ( [productID] => Array ( [0] => 2338117 [1] => 51154 ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/315167-how-to-process-the-multi-dimensional-array-into-specific-array/#findComment-1599243 Share on other sites More sharing options...
soyaslim Posted August 11, 2022 Author Share Posted August 11, 2022 Seriously, it has to be this simple I have been spending my whole day in this loop. I really appreciate this and thank u for saving my day. God bless u! Quote Link to comment https://forums.phpfreaks.com/topic/315167-how-to-process-the-multi-dimensional-array-into-specific-array/#findComment-1599244 Share on other sites More sharing options...
Barand Posted August 11, 2022 Share Posted August 11, 2022 It's the append that makes the difference. Your solution was always overwriting the product id that that was there. 1 Quote Link to comment https://forums.phpfreaks.com/topic/315167-how-to-process-the-multi-dimensional-array-into-specific-array/#findComment-1599245 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.