goosebriar Posted July 5, 2007 Share Posted July 5, 2007 Can anyone suggest a method for merging two different types of arrays? My first array: [0] => Array ( [1] => Array ( [time] => 1183638600 [type] => middle [name] => [date] => 8:30 [bgcolor] => 1 ) [2] => Array ( [time] => 1183640400 [type] => middle [name] => [date] => 9:00 [bgcolor] => 2 ) I need to look like this: [12] => Array ( [time] => 1183647600 [type] => [name] => [date] => 11:00 [bgcolor] => ) [13] => Array ( [time] => 1183649400 [type] => [name] => [date] => 11:30 [bgcolor] => ) So that I can merge the two to look like the second one. Any suggestions? Quote Link to comment Share on other sites More sharing options...
per1os Posted July 5, 2007 Share Posted July 5, 2007 <?php foreach ($first_array as $key => $array) { $new_array[$key] = $array; } print_r($new_array); ?> or www.php.net/array_merge Quote Link to comment Share on other sites More sharing options...
sasa Posted July 5, 2007 Share Posted July 5, 2007 $new_array=$old_array[0]; Quote Link to comment Share on other sites More sharing options...
goosebriar Posted July 8, 2007 Author Share Posted July 8, 2007 Thanks for your help, but neither worked. This solution: foreach ($first_array as $key => $array) { $new_array[$key] = $array; } created the same array I had in the first place, which was: [0] => Array ( [1182519000] => Array ( [time] => 1182519000 [type] => Middle [date] => 9:30 [bgcolor] => 0 ) [1182520800] => Array ( [time] => 1182520800 [type] => Middle [date] => 10:00 [bgcolor] => 0 ) [1182522600] => Array ( [time] => 1182522600 [type] => Middle [date] => 10:30 [bgcolor] => 0 ) ) [1] => Array ( [1182529800] => Array ( [time] => 1182529800 [type] => Middle [date] => 12:30 [bgcolor] => 1 ) [1182531600] => Array ( [time] => 1182531600 [type] => Middle [date] => 13:00 [bgcolor] => 1 ) ) The second solution: $new_array=$old_array[0]; gave me [1182519000] => Array ( [time] => 1182519000 [type] => Middle [date] => 9:30 [bgcolor] => 0 ) [1182520800] => Array ( [time] => 1182520800 [type] => Middle [date] => 10:00 [bgcolor] => 0 ) [1182522600] => Array ( [time] => 1182522600 [type] => Middle [date] => 10:30 [bgcolor] => 0 ) which was only one of the two keys I needed. I am stumped. I've been working on this all weekend (ignoring my husband) going through several different looping situations to try to solve it, but haven't been able to. Any other suggestions? Quote Link to comment Share on other sites More sharing options...
Barand Posted July 8, 2007 Share Posted July 8, 2007 What does the array really look like - it's different every time you post? Do var_export($your_array); and post the output. Quote Link to comment Share on other sites More sharing options...
goosebriar Posted July 8, 2007 Author Share Posted July 8, 2007 My merged array looks like this: Var export of array: array ( 0 => array ( 1182529800 => array ( 'time' => 1182529800, 'type' => 'Middle', 'date' => '12:30', 'bgcolor' => 1, ), 1182531600 => array ( 'time' => 1182531600, 'type' => 'Middle', 'date' => '13:00', 'bgcolor' => 1, ), ), 1 => array ( 1182519000 => array ( 'time' => 1182519000, 'type' => 'Middle', 'date' => '9:30', 'bgcolor' => 0, ), 1182520800 => array ( 'time' => 1182520800, 'type' => 'Middle', 'date' => '10:00', 'bgcolor' => 0, ), 1182522600 => array ( 'time' => 1182522600, 'type' => 'Middle', 'date' => '10:30', 'bgcolor' => 0, ), ), 2 => array ( 'time' => '1182517200', 'type' => 'Start', 'date' => '9:00', 'bgcolor' => 0, ), 3 => array ( 'time' => '1182524400', 'type' => 'End', 'date' => '11:00', 'bgcolor' => 0, ), 4 => array ( 'time' => '1182528000', 'type' => 'Start', 'date' => '12:00', 'bgcolor' => 1, ), 5 => array ( 'time' => '1182533400', 'type' => 'End', 'date' => '13:30', 'bgcolor' => 1, ), ) Index 0 and 1 above need to be on the same dimension as 2 and above so that I can sort them put them into a table. What I am trying to do is make a day view of a calendar that would eventually look like this: 9:00Start 9:30Middle 10:00Middle 10:30Middle 11:00End 12:00Start 12:30Middle 13:00Middle 13:30End And my current approach to get the day view (after several attempts at different approaches!) is to make arrays of the start and end time (which I have in a database) and then merge them, which works fine except that to create the middle range I have to create an additional array which calculates a range of middle times for each record in the database (so, for example, the first record with a start and end time in my database created 3 middle range elements) and, as you can see from above, that turns out as an associative array where as the start and end arrays are not. Therein lies my dilemma. Quote Link to comment Share on other sites More sharing options...
sasa Posted July 8, 2007 Share Posted July 8, 2007 try <?php $old_arraj = array ( 0 => array ( 1182529800 => array ( 'time' => 1182529800, 'type' => 'Middle', 'date' => '12:30', 'bgcolor' => 1, ), 1182531600 => array ( 'time' => 1182531600, 'type' => 'Middle', 'date' => '13:00', 'bgcolor' => 1, ), ), 1 => array ( 1182519000 => array ( 'time' => 1182519000, 'type' => 'Middle', 'date' => '9:30', 'bgcolor' => 0, ), 1182520800 => array ( 'time' => 1182520800, 'type' => 'Middle', 'date' => '10:00', 'bgcolor' => 0, ), 1182522600 => array ( 'time' => 1182522600, 'type' => 'Middle', 'date' => '10:30', 'bgcolor' => 0, ), ), 2 => array ( 'time' => '1182517200', 'type' => 'Start', 'date' => '9:00', 'bgcolor' => 0, ), 3 => array ( 'time' => '1182524400', 'type' => 'End', 'date' => '11:00', 'bgcolor' => 0, ), 4 => array ( 'time' => '1182528000', 'type' => 'Start', 'date' => '12:00', 'bgcolor' => 1, ), 5 => array ( 'time' => '1182533400', 'type' => 'End', 'date' => '13:30', 'bgcolor' => 1, ), ); $new_array =array(); foreach ($old_arraj as $old) if (array_key_exists('time',$old)) $new_array[] = $old; else $new_array = array_merge($new_array,$old); array_multisort($new_array); foreach ($new_array as $value) echo $value['date'], ' - ', $value['type'], "<br />\n"; ?> Quote Link to comment Share on other sites More sharing options...
goosebriar Posted July 9, 2007 Author Share Posted July 9, 2007 That worked!! It created some duplicates, but I was able to unset those. Thanks so much for your help and for being there on a weekend! 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.