james182 Posted July 12, 2013 Share Posted July 12, 2013 (edited) I have a time array and a event array and i need the 2 to join, could i get some help. So basically the All the times need be there and if there is a match event then it shown and if not nothing.What i have so far: Time Array Array ( [06:00:00] => 06:00:00 [07:00:00] => 07:00:00 [08:00:00] => 08:00:00 [09:00:00] => 09:00:00 [09:30:00] => 09:30:00 [17:30:00] => 17:30:00 [18:00:00] => 18:00:00 [18:30:00] => 18:30:00 [19:30:00] => 19:30:00 ) Event Array Array ( [0] => stdClass Object ( [class_id] => 1 [class_name] => Fit Box [class_description] => Fitbox is a high energy aerobic workout utilizing focus pads, kick pads, heavy bags, and speed balls. This class increases muscle strength and cardiovascular fitness and also includes strength and endurance circuit style training. Excellent for co-ordination, reflexes and to pump out the adrenalin! The class is 1 hour in duration. [class_time] => 06:00:00 [class_day] => Tuesday [class_status] => active [class_colour] => blue ) [1] => stdClass Object ( [class_id] => 2 [class_name] => Hot Boxing [class_description] => test description [class_time] => 08:00:00 [class_day] => Wednesday [class_status] => active [class_colour] => grey ) [2] => stdClass Object ( [class_id] => 3 [class_name] => Punch Face [class_description] => test again [class_time] => 09:00:00 [class_day] => Thursday [class_status] => active [class_colour] => grey ) [3] => stdClass Object ( [class_id] => 4 [class_name] => MOS [class_description] => test again [class_time] => 19:30:00 [class_day] => Monday [class_status] => active [class_colour] => yellow ) [4] => stdClass Object ( [class_id] => 5 [class_name] => Yoga [class_description] => test description [class_time] => 08:00:00 [class_day] => Wednesday [class_status] => active [class_colour] => grey ) ) The Code that aims at displaying all the times and each time has all the days, then each day needs to show events if they have them. $result_array = array(); $days_array = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); foreach($timetable_times as $time_key => $time_value) { // initialize all the days of the week for each time entry $result_array[$time_value['time']] = array(); foreach($days_array as $day) { $result_array[$time_value['time']][$day] = ""; } if (array_key_exists($day, $timetable_classes)) { $event_entry = $timetable_classes[$time_value['time']]; foreach($event_entry as $event_day => $events) { $result_array[$time_value['time']][$day][] = $events; } } } print_r($result_array); My desired outcome is below $arr = array( "06:00:00" => array( "Sunday" => array( array( 'event_title' => "item_1", 'event_desc' => "item_1", 'event_link' => "item_1", ), ), "Monday" => array( array( 'event_title' => "item_1", 'event_desc' => "item_1", 'event_link' => "item_1", ), array( 'event_title' => "item_1", 'event_desc' => "item_1", 'event_link' => "item_1", ), ), "Tuesday" => "", "Wednesday" => "", "Thursday" => "", "Friday" => "", "Saturday" => "" ), "07:00:00" => array("Sunday" => "", "Monday" => "", "Tuesday" => "", "Wednesday" => "", "Thursday" => "", "Friday" => "", "Saturday" => ""), "08:00:00" => array("Sunday" => "", "Monday" => "", "Tuesday" => "", "Wednesday" => "", "Thursday" => "", "Friday" => "", "Saturday" => ""), "09:30:00" => array("Sunday" => "", "Monday" => "", "Tuesday" => "", "Wednesday" => "", "Thursday" => "", "Friday" => "", "Saturday" => ""), "17:00:00" => array("Sunday" => "", "Monday" => "", "Tuesday" => "", "Wednesday" => "", "Thursday" => "", "Friday" => "", "Saturday" => ""), ); Edited July 12, 2013 by james182 Quote Link to comment https://forums.phpfreaks.com/topic/280088-php-arrays-help/ Share on other sites More sharing options...
.josh Posted July 12, 2013 Share Posted July 12, 2013 Okay so your example data doesn't match your desired output so I made some guesses and assumptions.. also my code example just uses some arrays (I left them as close to the structure as your objects for easier translation); you'll have to modify it to work with your object(s) but hopefully this will point you in the right direction. On a side note..I don't know the context of your script so maybe that's what works for you, but just throwin' it out there that it's kind of odd that you want the hierarchy to be time > days instead of days > time.. $times = array( '06:00:00' => '06:00:00', '07:00:00' => '07:00:00', '08:00:00' => '08:00:00', '09:00:00' => '09:00:00', '09:30:00' => '09:30:00', '17:30:00' => '17:30:00', '18:00:00' => '18:00:00', '18:30:00' => '18:30:00', '19:30:00' => '19:30:00' ); $events = array( 0 => array( 'class_id' => '1', 'class_name' => 'Fit Box', 'class_description' => 'Fitbox is a high energy aerobic workout utilizing focus pads, kick pads, heavy bags, and speed balls. This class increases muscle strength and cardiovascular fitness and also includes strength and endurance circuit style training. Excellent for co-ordination, reflexes and to pump out the adrenalin! The class is 1 hour in duration.', 'class_time' => '06:00:00', 'class_day' => 'Tuesday', 'class_status' => 'active', 'class_colour' => 'blue', ), 1 => array( 'class_id' => '2', 'class_name' => 'Hot Boxing', 'class_description' => 'test description', 'class_time' => '08:00:00', 'class_day' => 'Wednesday', 'class_status' => 'active', 'class_colour' => 'grey' ), 2 => array( 'class_id' => '3', 'class_name' => 'Punch Face', 'class_description' => 'test again', 'class_time' => '09:00:00', 'class_day' => 'Thursday', 'class_status' => 'active', 'class_colour' => 'grey' ), 3 => array( 'class_id' => '4', 'class_name' => 'MOS', 'class_description' => 'test again', 'class_time' => '19:30:00', 'class_day' => 'Monday', 'class_status' => 'active', 'class_colour' => 'yellow' ), 4 => array( 'class_id' => '5', 'class_name' => 'Yoga', 'class_description' => 'test description', 'class_time' => '08:00:00', 'class_day' => 'Wednesday', 'class_status' => 'active', 'class_colour' => 'grey' ) ); $days_array = array("Sunday"=>"", "Monday"=>"", "Tuesday"=>"", "Wednesday"=>"", "Thursday"=>"", "Friday"=>"", "Saturday"=>""); $result_array = array(); foreach ($times as $time) { $result_array[$time] = $days_array; } foreach ($events as $event) { $class_time = $event['class_time']; $class_day = $event['class_day']; if ( isset($result_array[$class_time]) ) { $result_array[$class_time][$class_day][] = array( 'event_title' => $event['class_name'], 'event_desc' => $event['class_description'], 'event_link' => $event['class_link'] ); } } echo "<pre>";print_r($result_array); output: Array ( [06:00:00] => Array ( [Sunday] => [Monday] => [Tuesday] => Array ( [0] => Array ( [event_title] => Fit Box [event_desc] => Fitbox is a high energy aerobic workout utilizing focus pads, kick pads, heavy bags, and speed balls. This class increases muscle strength and cardiovascular fitness and also includes strength and endurance circuit style training. Excellent for co-ordination, reflexes and to pump out the adrenalin! The class is 1 hour in duration. [event_link] => ) ) [Wednesday] => [Thursday] => [Friday] => [Saturday] => ) [07:00:00] => Array ( [Sunday] => [Monday] => [Tuesday] => [Wednesday] => [Thursday] => [Friday] => [Saturday] => ) [08:00:00] => Array ( [Sunday] => [Monday] => [Tuesday] => [Wednesday] => Array ( [0] => Array ( [event_title] => Hot Boxing [event_desc] => test description [event_link] => ) [1] => Array ( [event_title] => Yoga [event_desc] => test description [event_link] => ) ) [Thursday] => [Friday] => [Saturday] => ) [09:00:00] => Array ( [Sunday] => [Monday] => [Tuesday] => [Wednesday] => [Thursday] => Array ( [0] => Array ( [event_title] => Punch Face [event_desc] => test again [event_link] => ) ) [Friday] => [Saturday] => ) [09:30:00] => Array ( [Sunday] => [Monday] => [Tuesday] => [Wednesday] => [Thursday] => [Friday] => [Saturday] => ) [17:30:00] => Array ( [Sunday] => [Monday] => [Tuesday] => [Wednesday] => [Thursday] => [Friday] => [Saturday] => ) [18:00:00] => Array ( [Sunday] => [Monday] => [Tuesday] => [Wednesday] => [Thursday] => [Friday] => [Saturday] => ) [18:30:00] => Array ( [Sunday] => [Monday] => [Tuesday] => [Wednesday] => [Thursday] => [Friday] => [Saturday] => ) [19:30:00] => Array ( [Sunday] => [Monday] => Array ( [0] => Array ( [event_title] => MOS [event_desc] => test again [event_link] => ) ) [Tuesday] => [Wednesday] => [Thursday] => [Friday] => [Saturday] => ) ) Quote Link to comment https://forums.phpfreaks.com/topic/280088-php-arrays-help/#findComment-1440421 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.