2013-07-12 174 views
0

我想要實現的是顯示所有會話時間並且每次都有所有星期幾,然後每個星期一天將顯示一個或多個事件(如果他們有)。PHP陣列合併

我可以得到時間和週日,但不是那些日子的事件。我提供了我的數組和代碼,看看我能否得到一些幫助。

我的會話超時

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 
) 

我的活動

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 
) 

) 

這是我的代碼,試圖做的工作。

$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); 

我很感激幫助。

這是我的目標爲

$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" => ""), 
            ); 
+0

你在說什麼?這是讓我瘋狂 – James182

+0

你可以嘗試正確縮進你的代碼,使其更具可讀性?不知道爲什麼你會得票。 – 2013-07-12 02:46:52

+0

爲您縮進代碼 – James182

回答

1

首先成果創造,將搜索列表匹配平日和時隙 項功能(帶密封蓋這種實現需要PHP 5.3,但對於舊版本的實現應該是微不足道的)

<?php 
function getClassesAt($day, $timeslot, $classes){ 
    $matches = array_filter($classes, function($class) use ($day, $timeslot){ 
     return ($class->class_day == $day && $class->class_time == $timeslot); 
    }); 
    if(count($matches) == 0) return ""; 
    $ret = array(); 

    foreach($matches as $match){ 
     $ret[] = array("title"=>$class->class_name, 
         "description"=>$class->class_description, 
         "link"=>"???"); 
    } 
    return $ret; 
} 

然後建立具有與平日密鑰對每個會話密鑰的陣列的容器陣列

$days = array("Monday"=>"","Tuesday"=>"","Wednesday"=>"","Thursday"=>"","Friday"=>"","Saturday"=>"","Sunday"=>""); 
$results = array_combine($sessions, array_pad(array(), count($sessions), $days)); 

上述代碼的最後一行的替代將是

$results = array(); 
foreach ($sessions as $session){ 
    $results[$session] = $days; 
} 

然後走進行陣列呼叫和分配的getClassesAt功能的葉子。

foreach($results as $timeslot =>&$weekdays){ 
    foreach ($weekdays as $weekday=>$res){ 
     $weekdays[$weekday] = getClassAt($weekday, $timeslot, $classes); 
    } 
} 

警告這只是部分測試,所以我只有約60%肯定它會做你想要什麼,但它是基於邏輯可能指向你在正確的方向。

UPDATE

基於此新提供的代碼是一個可行的解決方案。

<?php 
$classes = array(
        array(
         "class_id" => 1, 
         "class_name" => "Fit Box", 
         "class_description" => "description here", 
         "class_time" => "06:00:00", 
         "class_day" => "Tuesday", 
         "class_status" => "active", 
         "class_colour" => "blue", 
        ), 
        array(
         "class_id" => 2, 
         "class_name" => "Hot Box", 
         "class_description" => "description here", 
         "class_time" => "08:00:00", 
         "class_day" => "Wednesday", 
         "class_status" => "active", 
         "class_colour" => "Red", 
        ), 
        array(
         "class_id" => 3, 
         "class_name" => "Punch Face", 
         "class_description" => "description here", 
         "class_time" => "09:00:00", 
         "class_day" => "Thursday", 
         "class_status" => "active", 
         "class_colour" => "blue", 
        ), 
        array(
         "class_id" => 4, 
         "class_name" => "MOS", 
         "class_description" => "description here", 
         "class_time" => "19:30:00", 
         "class_day" => "Monday", 
         "class_status" => "active", 
         "class_colour" => "blue", 
        ), 
        array(
         "class_id" => 5, 
         "class_name" => "Yoga", 
         "class_description" => "description here", 
         "class_time" => "08:00:00", 
         "class_day" => "Wednesday", 
         "class_status" => "active", 
         "class_colour" => "blue", 
        ), 
       ); 
$timetable_times = array(
        array(
         "time_id" => 1, 
         "time" => "06:00:00", 
        ), 
        array(
         "time_id" => 2, 
         "time" => "07:00:00", 
        ), 
        array(
         "time_id" => 3, 
         "time" => "08:00:00", 
        ), 
        array(
         "time_id" => 4, 
         "time" => "09:00:00", 
        ), 
        array(
         "time_id" => 5, 
         "time" => "09:30:00", 
        ), 
        array(
         "time_id" => 6, 
         "time" => "17:30:00", 
        ), 
        array(
         "time_id" => 7, 
         "time" => "18:00:00", 
        ), 
        array(
         "time_id" => 8, 
         "time" => "18:30:00", 
        ), 
        array(
         "time_id" => 9, 
         "time" => "19:30:00", 
        ), 
       ); 


function getClassesAt($day, $timeslot, $classes){ 
    $matches = array_filter($classes, function($class) use ($day, $timeslot){ 
     return ($class["class_day"] == $day && $class["class_time"] == $timeslot); 
    }); 
    if(count($matches) == 0) return ""; 
    $ret = array(); 

    foreach($matches as $match){ 
     $ret[] = array("title"=>$match["class_name"], 
         "description"=>$match["class_description"], 
         "link"=>"???"); 
    } 
    return $ret; 
} 


$sessions = array_map(function($a){return $a["time"]; }, $timetable_times);  
$days = array("Monday"=>"","Tuesday"=>"","Wednesday"=>"","Thursday"=>"","Friday"=>"","Saturday"=>"","Sunday"=>""); 
$results = array_combine($sessions, array_pad(array(), count($sessions), $days)); 

foreach($results as $timeslot =>&$weekdays){ 
    foreach ($weekdays as $weekday=>$res){ 
     $weekdays[$weekday] = getClassesAt($weekday, $timeslot, $classes); 
    } 
} 

print_r($results); 

輸出是

Array 
(
    [06:00:00] => Array 
     (
      [Monday] => 
      [Tuesday] => Array 
       (
        [0] => Array 
         (
          [title] => Fit Box 
          [description] => description here 
          [link] => ??? 
         ) 

       ) 

      [Wednesday] => 
      [Thursday] => 
      [Friday] => 
      [Saturday] => 
      [Sunday] => 
     ) 

    [07:00:00] => Array 
     (
      [Monday] => 
      [Tuesday] => 
      [Wednesday] => 
      [Thursday] => 
      [Friday] => 
      [Saturday] => 
      [Sunday] => 
     ) 

    [08:00:00] => Array 
     (
      [Monday] => 
      [Tuesday] => 
      [Wednesday] => Array 
       (
        [0] => Array 
         (
          [title] => Hot Box 
          [description] => description here 
          [link] => ??? 
         ) 

        [1] => Array 
         (
          [title] => Yoga 
          [description] => description here 
          [link] => ??? 
         ) 

       ) 

      [Thursday] => 
      [Friday] => 
      [Saturday] => 
      [Sunday] => 
     ) 

    [09:00:00] => Array 
     (
      [Monday] => 
      [Tuesday] => 
      [Wednesday] => 
      [Thursday] => Array 
       (
        [0] => Array 
         (
          [title] => Punch Face 
          [description] => description here 
          [link] => ??? 
         ) 

       ) 

      [Friday] => 
      [Saturday] => 
      [Sunday] => 
     ) 

    [09:30:00] => Array 
     (
      [Monday] => 
      [Tuesday] => 
      [Wednesday] => 
      [Thursday] => 
      [Friday] => 
      [Saturday] => 
      [Sunday] => 
     ) 

    [17:30:00] => Array 
     (
      [Monday] => 
      [Tuesday] => 
      [Wednesday] => 
      [Thursday] => 
      [Friday] => 
      [Saturday] => 
      [Sunday] => 
     ) 

    [18:00:00] => Array 
     (
      [Monday] => 
      [Tuesday] => 
      [Wednesday] => 
      [Thursday] => 
      [Friday] => 
      [Saturday] => 
      [Sunday] => 
     ) 

    [18:30:00] => Array 
     (
      [Monday] => 
      [Tuesday] => 
      [Wednesday] => 
      [Thursday] => 
      [Friday] => 
      [Saturday] => 
      [Sunday] => 
     ) 

    [19:30:00] => Array 
     (
      [Monday] => Array 
       (
        [0] => Array 
         (
          [title] => MOS 
          [description] => description here 
          [link] => ??? 
         ) 

       ) 

      [Tuesday] => 
      [Wednesday] => 
      [Thursday] => 
      [Friday] => 
      [Saturday] => 
      [Sunday] => 
     ) 

) 
+0

首先我得到一個_Array到字符串conversion_錯誤與此行$ result = array_combine($會議,array_pad(陣列(),計數($會議),$天));' – James182

+0

這一個作品對我來說......你在運行什麼版本的PHP? – Orangepill

+0

發佈了一個替代實施方式的線的引發錯誤 – Orangepill