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? Link to comment https://forums.phpfreaks.com/topic/211124-array-building/ 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']; } Link to comment https://forums.phpfreaks.com/topic/211124-array-building/#findComment-1101021 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 ) ) ) Link to comment https://forums.phpfreaks.com/topic/211124-array-building/#findComment-1101108 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 Link to comment https://forums.phpfreaks.com/topic/211124-array-building/#findComment-1101151 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', ), ), ) Link to comment https://forums.phpfreaks.com/topic/211124-array-building/#findComment-1101331 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? Link to comment https://forums.phpfreaks.com/topic/211124-array-building/#findComment-1101340 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! Link to comment https://forums.phpfreaks.com/topic/211124-array-building/#findComment-1101383 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.