Jump to content


Photo

PHP Arrays Help


  • Please log in to reply
1 reply to this topic

#1 james182

james182

    Advanced Member

  • Members
  • PipPipPip
  • 49 posts

Posted 11 July 2013 - 07:15 PM

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 by james182, 11 July 2013 - 07:23 PM.


#2 .josh

.josh

    .josh

  • Staff Alumni
  • 14,820 posts

Posted 11 July 2013 - 11:32 PM

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] => 
        )

)


Did I help you? Feeling generous? Donate to me! || Donate to phpfreaks!
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com