ShibSta Posted October 22, 2007 Share Posted October 22, 2007 I have a small question... If I have the 2 following array's how can I make them into one? $temp_data = array ('2007' => array ( '10' => array ( '20' => array ( "Uniques" => $temp_uniques ), ), ), ); $temp_data2 = array ('2007' => array ( '10' => array ( '21' => array ( "Uniques" => $temp_uniques ), ), ), ); I want it to become: $temp_data = array ('2007' => array ( '10' => array ( '20' => array ( "Uniques" => $temp_uniques ), ), '21' => array ( "Uniques" => $temp_uniques ), ), ), ); (The array's will be generated at separate times so It's not static code... Otherwise I'd put them in 1 array to being with... However, I am not sure what array function I would need to use... Thanks, - ShibSta Quote Link to comment https://forums.phpfreaks.com/topic/74270-solved-php-arrays/ Share on other sites More sharing options...
tibberous Posted October 22, 2007 Share Posted October 22, 2007 I think you want to do an array_merge($temp_data['2007']['10'], $temp_data2['2007']['10']); Quote Link to comment https://forums.phpfreaks.com/topic/74270-solved-php-arrays/#findComment-375246 Share on other sites More sharing options...
ShibSta Posted October 22, 2007 Author Share Posted October 22, 2007 I think you want to do an array_merge($temp_data['2007']['10'], $temp_data2['2007']['10']); Problem is the array is dynamically created and will variate.. So it will be like: $temp_data = array ('2007' => array ( '10' => array ( '30' => array ( "Uniques" => $temp_uniques ), ), ), ); $temp_data2 = array ('2007' => array ( '10' => array ( '31' => array ( "Uniques" => $temp_uniques ), ), ), ); $temp_data3 = array ('2007' => array ( '11' => array ( '01' => array ( "Uniques" => $temp_uniques ), ), ), ); $temp_data3 = array ('2008' => array ( '01' => array ( '01' => array ( "Uniques" => $temp_uniques ), ), ), ); I would need this to become: $temp_data = array ('2007' => array ( '10' => array ( '30' => array ( "Uniques" => $temp_uniques ), ), '31' => array ( "Uniques" => $temp_uniques ), ), '11' => array ( '01' => array ( "Uniques" => $temp_uniques ), ), ), '2008' => array ( '01' => array ( '01' => array ( "Uniques" => $temp_uniques ), ), ); So I need it to merge the entire base array if possible... Quote Link to comment https://forums.phpfreaks.com/topic/74270-solved-php-arrays/#findComment-375265 Share on other sites More sharing options...
Barand Posted October 22, 2007 Share Posted October 22, 2007 array_merge($temp_data, $temp_data2); Quote Link to comment https://forums.phpfreaks.com/topic/74270-solved-php-arrays/#findComment-375303 Share on other sites More sharing options...
ShibSta Posted October 22, 2007 Author Share Posted October 22, 2007 array_merge($temp_data, $temp_data2); Thanks, but that is 1) renaming the keys and 2) not merging them the way I want... It outputs: Array ( [0] => Array ( [10] => Array ( [01] => Array ( [uniques] => 449.0 ) ) ) [1] => Array ( [10] => Array ( [02] => Array ( [uniques] => 771.0 ) ) ) ) I need it to output: Array ( [2007] => Array ( [10] => Array ( [01] => Array ( [uniques] => 449.0 ) ) [10] => Array ( [02] => Array ( [uniques] => 771.0 ) ) ) ) (Ignore the Uniques Value...) Grats on your 11,500th post... Quote Link to comment https://forums.phpfreaks.com/topic/74270-solved-php-arrays/#findComment-375304 Share on other sites More sharing options...
Barand Posted October 22, 2007 Share Posted October 22, 2007 try <?php $temp_data = array ('2007' => array ( '10' => array ( '30' => array ( "Uniques" => $temp_uniques ), ), ), ); $temp_data2 = array ('2007' => array ( '10' => array ( '31' => array ( "Uniques" => $temp_uniques ), ), ), ); $temp_data3 = array ('2007' => array ( '11' => array ( '01' => array ( "Uniques" => $temp_uniques ), ), ), ); $temp_data4 = array ('2008' => array ( '01' => array ( '01' => array ( "Uniques" => $temp_uniques ), ), ), ); function my_merge (&$data, &$data2) { foreach ($data2 as $y => $ydata) { foreach ($ydata as $m => $mdata) { foreach ($mdata as $d => $ddata) { $data[$y][$m][$d] = $ddata; } } } } my_merge ($temp_data, $temp_data2); my_merge ($temp_data, $temp_data3); my_merge ($temp_data, $temp_data4); echo '<pre>', print_r($temp_data, true), '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/74270-solved-php-arrays/#findComment-375309 Share on other sites More sharing options...
ShibSta Posted October 22, 2007 Author Share Posted October 22, 2007 Hmm, seems like a "hack" method of accomplishing it. But it works as needed. Thanks, - ShibSta Quote Link to comment https://forums.phpfreaks.com/topic/74270-solved-php-arrays/#findComment-375319 Share on other sites More sharing options...
Barand Posted October 22, 2007 Share Posted October 22, 2007 If you have something more elegant, do share. Solve your own problems in future, I certainly won't be. Quote Link to comment https://forums.phpfreaks.com/topic/74270-solved-php-arrays/#findComment-375326 Share on other sites More sharing options...
ShibSta Posted October 22, 2007 Author Share Posted October 22, 2007 If you have something more elegant, do share. Solve your own problems in future, I certainly won't be. I was not trying to be rude, sorry if I came across that way. I am very thankful for your help & support. - ShibSta Quote Link to comment https://forums.phpfreaks.com/topic/74270-solved-php-arrays/#findComment-375594 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.