Jump to content

Array Building


StevenAFC

Recommended Posts

Hi All,

 

I have an array like this where the cars are the first level of the array, and the owners are children of that first level:

 

[Car 1]

          ----[Owner 1]

[Car 2]

          ----[Owner 2]

[Car 2]

          ----[Owner 3]

[Car 2]

          ----[Owner 4]

 

I want to convert it to:

 

[Car 1]

          ----[Owner 1]

[Car 2]

          ----[Owner 2]

          ----[Owner 3]

          ----[Owner 4]

 

So I started making this script:

 

$cars = array();			
foreach($owners as $owner) {
if(!in_array($owner['Car'], $cars)) {
	$cars[] = $owner['Car'];
}

echo array_search($owner['Car'], $cars, true);

$cars[array_search($owner['Car'], $cars)]['Owner'][] = $owner['Owner'];
}

 

The result of this is the same as the beginning result, any ideas what could be causing that?

 

Link to comment
Share on other sites

I get invalid offset if I use something like that, this is a print_r of the array:

 

Array
(
    [0] => Array
        (
            [Car] => Array
                (
                    [id] => 2
                    [name] => BMW
                    [created] => 2010-08-15 22:44:26
                    [modified] => 2010-08-15 22:47:04
                )

            [Owner] => Array
                (
                    [id] => 4
                    [car_id] => 2
                    [name] => Bob
                    [created] => 2010-08-18 22:15:45
                    [modified] => 2010-08-18 22:15:45
                    [user_id] => 1
                )

        )

    [1] => Array
        (
            [Car] => Array
                (
                    [id] => 4
                    [name] => Mercedes
                    [created] => 2010-08-16 21:26:01
                    [modified] => 2010-08-16 21:26:01
                )

            [Owner] => Array
                (
                    [id] => 4
                    [car_id] => 2
                    [name] => Jules
                    [created] => 2010-08-18 22:15:45
                    [modified] => 2010-08-18 22:15:45
                    [user_id] => 1
                )

        )

    [2] => Array
        (
            [Car] => Array
                (
                    [id] => 4
                    [name] => Mercedes
                    [created] => 2010-08-16 21:26:01
                    [modified] => 2010-08-16 21:26:01
                )

            [Owner] => Array
                (
                    [id] => 4
                    [car_id] => 2
                    [name] => Simbad
                    [created] => 2010-08-18 22:15:45
                    [modified] => 2010-08-18 22:15:45
                    [user_id] => 1
                )

        )

    [3] => Array
        (
            [Car] => Array
                (
                    [id] => 4
                    [name] => Mercedes
                    [created] => 2010-08-16 21:26:01
                    [modified] => 2010-08-16 21:26:01
                )

            [Owner] => Array
                (
                    [id] => 4
                    [car_id] => 2
                    [name] => El Salvador
                    [created] => 2010-08-18 22:15:45
                    [modified] => 2010-08-18 22:15:45
                    [user_id] => 1
                )
        )
)

Link to comment
Share on other sites

array ( 0 => array ( 'Car' => array ( 'id' => '2', 'name' => 'BMW', ), 'Owner' => array ( 'id' => '4', 'car_id' => '2', 'name' => 'Bob', ), ), 1 => array ( 'Car' => array ( 'id' => '4', 'name' => 'Mercedes', ), 'Owner' => array ( 'id' => '4', 'car_id' => '4', 'name' => 'Jules', ), ), 2 => array ( 'Car' => array ( 'id' => '4', 'name' => 'Mercedes', ), 'Owner' => array ( 'id' => '4', 'car_id' => '4', 'name' => 'Simbad', ), ), 3 => array ( 'Car' => array ( 'id' => '4', 'name' => 'Mercedes', ), 'Owner' => array ( 'id' => '4', 'car_id' => '4', 'name' => 'El Savador', ), ), )

Link to comment
Share on other sites

$r = array ( 0 => array ( 'Car' => array ( 'id' => '2', 'name' => 'BMW', ), 'Owner' => array ( 'id' => '4', 'car_id' => '2', 'name' => 'Bob', ), ), 1 => array ( 'Car' => array ( 'id' => '4', 'name' => 'Mercedes', ), 'Owner' => array ( 'id' => '4', 'car_id' => '4', 'name' => 'Jules', ), ), 2 => array ( 'Car' => array ( 'id' => '4', 'name' => 'Mercedes', ), 'Owner' => array ( 'id' => '4', 'car_id' => '4', 'name' => 'Simbad', ), ), 3 => array ( 'Car' => array ( 'id' => '4', 'name' => 'Mercedes', ), 'Owner' => array ( 'id' => '4', 'car_id' => '4', 'name' => 'El Savador', ), ), );

$cars = array();
foreach($r as $v) {
    if(isset($cars[$v['Car']['id']])) {
        $cars[$v['Car']['id']]['Owners'][] = $v['Owner'];
    }else{
        $cars[$v['Car']['id']] = array('Car' => $v['Car'], 'Owners' => array($v['Owner']));
    }
}
echo '<pre>'.print_r($cars, true).'</pre>';

 

Is that more what you had in mind?

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.