StevenAFC Posted August 18, 2010 Share Posted August 18, 2010 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? Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted August 18, 2010 Share Posted August 18, 2010 Why not just use $cars = array(); foreach($owners as $owner) { $cars[$owner['Car']]['Owner'][] = $owner['Owner']; } Quote Link to comment Share on other sites More sharing options...
StevenAFC Posted August 19, 2010 Author Share Posted August 19, 2010 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 ) ) ) Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted August 19, 2010 Share Posted August 19, 2010 can you use var_export($owners); and paste the code it generates Quote Link to comment Share on other sites More sharing options...
StevenAFC Posted August 19, 2010 Author Share Posted August 19, 2010 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', ), ), ) Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted August 19, 2010 Share Posted August 19, 2010 $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? Quote Link to comment Share on other sites More sharing options...
StevenAFC Posted August 19, 2010 Author Share Posted August 19, 2010 Perfect! Thank you very much for your help! Quote Link to comment 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.