Jump to content

[SOLVED] Sort Array


Wallzy

Recommended Posts

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
Share on other sites

<?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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.