Wallzy Posted September 28, 2007 Share Posted September 28, 2007 Hi, I am a newbie to PHP. I would like to do something with an array but I am not sure how exactly I should go about it so any pointers/help would be greatly appriciated. I have an array of vehicle details as follows: $vehicleDetails[0] ('id' = '1', 'make' = 'VW', 'model' = 'Golf', 'color = 'silver', 'doors' = '5') $vehicleDetails[1] ('id' = '2', 'make' = 'Audi', 'model' = 'A3', 'color = 'black', 'doors' = '3') $vehicleDetails[2] ('id' = '3', 'make' = 'VW', 'model' = 'Passat', 'color = 'red', 'doors' = '4') $vehicleDetails[3] ('id' = '4', 'make' = 'Audi', 'model' = 'A4', 'color = 'blue', 'doors' = '5') $vehicleDetails[4] ('id' = '4', 'make' = 'Audi', 'model' = 'A6', 'color = 'white', 'doors' = '4) I would like to be able to take this array and from it produce a new associative array which has $vehicle[0]["VW"] $vehicleDetails[0] ('id' = '1', 'make' = 'VW', 'model' = 'Golf', 'color = 'silver', 'doors' = '5') $vehicle[0]["VW"] $vehicleDetails[1] ('id' = '1', 'make' = 'VW', 'model' = 'Golf', 'color = 'silver', 'doors' = '5') $vehicle[1]["Audi"] $vehicleDetails[0] ('id' = '2', 'model' = 'A3', 'color = 'black', 'doors' = '3') $vehicle[1]["Audi"] $vehicleDetails[1] ('id' = '4', 'model' = 'A4', 'color = 'blue', 'doors' = '5') $vehicle[1]["Audi"] $vehicleDetails[2] ('id' = '4', 'model' = 'A6', 'color = 'white', 'doors' = '4) So that I have all of the VW vehicle details grouped together in the $vehicle array at position 1 with a sub array holding the details of the first VW vehicle, another sub array hold the second VW vehicle details and so on. Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/71047-solved-sort-array/ Share on other sites More sharing options...
Barand Posted September 28, 2007 Share Posted September 28, 2007 <?php $vehicleDetails[0] =array ('id' => '1', 'make' => 'VW', 'model' => 'Golf', 'color' => 'silver', 'doors' => '5'); $vehicleDetails[1] =array ('id' => '2', 'make' => 'Audi', 'model' => 'A3', 'color' => 'black', 'doors' => '3') ; $vehicleDetails[2] =array ('id' => '3', 'make' => 'VW', 'model' => 'Passat', 'color' => 'red', 'doors' => '4') ; $vehicleDetails[3] =array ('id' => '4', 'make' => 'Audi', 'model' => 'A4', 'color' => 'blue', 'doors' => '5') ; $vehicleDetails[4] =array ('id' => '4', 'make' => 'Audi', 'model' => 'A6', 'color' => 'white', 'doors' => '4') ; $byMake = array(); foreach($vehicleDetails as $cardata) { $byMake[$cardata['make']][] = array ( 'id' => $cardata['id'], 'model' => $cardata['model'], 'color' => $cardata['color'], 'doors' => $cardata['doors'] ); } echo '<pre>', print_r($byMake, true), '</pre>'; ?> --> [pre] Array ( [VW] => Array ( [0] => Array ( [id] => 1 [model] => Golf => silver [doors] => 5 ) [1] => Array ( [id] => 3 [model] => Passat => red [doors] => 4 ) ) [Audi] => Array ( [0] => Array ( [id] => 2 [model] => A3 => black [doors] => 3 ) [1] => Array ( [id] => 4 [model] => A4 => blue [doors] => 5 ) [2] => Array ( [id] => 4 [model] => A6 => white [doors] => 4 ) ) ) Link to comment https://forums.phpfreaks.com/topic/71047-solved-sort-array/#findComment-357268 Share on other sites More sharing options...
Wallzy Posted September 28, 2007 Author Share Posted September 28, 2007 Thanks a million. Works a treat. A useful one.....for me anyway Link to comment https://forums.phpfreaks.com/topic/71047-solved-sort-array/#findComment-357495 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.