2015-10-23 28 views
0

我需要一些帮助。这似乎很简单,但我无法完成它的正确。 这里是我的XML的结构将XML元素的值拆分为两部分,然后对它们进行分组

<events> 
<Show ID="1"> 
    <Time>2013-01-05 17:30:00</Time> 
</Show> 
<Show ID="2"> 
    <Time>2013-01-05 20:30:00</Time> 
</Show> 
<Show ID="3"> 
    <Time>2013-01-06 17:30:00</Time> 
</Show> 
<Show ID="4"> 
    <Time>2013-01-06 20:30:00</Time> 
</Show> 
<Show ID="5"> 
    <Time>2013-01-06 21:30:00</Time> 
</Show> 
<Show ID="6"> 
    <Time>2013-01-07 15:30:00</Time> 
</Show> 
</events> 

现在我需要做的,就是“时间”元素分裂成两个值:日期和时间:

<?php $xml = simplexml_load_file('file.xml'); 
foreach ( $xml->Show as $Show) 
    { 
    // ... 
    foreach  ( $Show->Time as $Date) 
     { 
     $Timecon = date_create_from_format('Y-m-d H:i:s', $Date); 
     $day = date_format($Timecon, 'd.m.'); 
     $hours = date_format($Timecon, 'H:i'); 
     } 
    } 

?> 

我饶了你休息的代码,因为我实际上报废了它 - 反正它是错误的,再加上我是一个noob: 将$ Date格式化为$ day和$ hours工作得很好,但对于分组 - 无论我做什么,这就是我得到:

05.01。 17:30
05.01。 20:30
06.01。 17:30
06.01。 20:30
06.01。 21:30
07.01。 15:30

这是我需要,因为XML可以得到相当长:

05.01。 17:30 20:30
06.01。 17:30 20:30 21:30
07.01。 15:30

正如您所看到的,我希望$小时按$ day分组,或者让每个重复$美元跳过/删除。 我宁愿用PHP做这个,而不用XSL。

是的,我GOOGLE了几个小时,感觉真的很愚蠢,没有得到它。感谢您的帮助!

+0

我提供了一个答案,澄清了一些日期操纵的东西,但不知道我明白你想要做什么。 – Chris

回答

0

创建一个数组来存储中的值。

$group = array(); 

foreach($Show->Time as $Date) { 

    $_date = new DateTime($Date); 

    //if this is a new day, add it to the group 
    if(! isset($group[$_date->format('m.d')])) 
     $group[ $_date->format('m.d') ] = array(); 

    //add the time to the array, organized by month.day 
    $group[ $_date->format('m.d') ][] = $_date->format('H:i'); 

} 

//now you can iterate through the array to print everything out by day/time 
foreach($group as $month => $times) { 
    echo $month . ': <br />'; 
    foreach($times as $time) { 
     echo $time . '<br />'; 
    } 
} 
+0

感谢你的帮助,但是这还推出: 01.05:17:30 01.05:20:30 01.06:20:30 01.06:21:30 01.07:20:30 以外 01.05:17 :30 20:30 01.06:20:30 21:30 01.07:20:30 – dnnr

+0

是的,它是因为我吐出来的例子。试试'print_r($ group)'。你可以迭代数组并根据你的需要做任何事情。 – Chris

+0

终于奏效!看到我下面的评论。非常感谢你! – dnnr

1

两个foreach循环的的外面创建一个空数组。

$grouped = array(); 

然后创建后您的变数..

$day = date_format($Timecon, 'd.m.'); 
$hours = date_format($Timecon, 'H:i').... 

然后按日期作为重点和时间的值。

$grouped[$day] = $hours; 

然后当你倾倒的数组,你就会有一个数组,看起来是这样的:

Array(
    [5.01] = Array(
     String [5] = '17:30', 
     String [5] = '20:30' 
    ), 
    [6.01] = Array(
     String [5] = '17:30', 
     String [5] = '20:30' 
    ), 
); 

等等。

+0

感谢您的帮助! – dnnr

1

我用克里斯的建议稍作改动,它的工作原理,谢谢!

$group = array(); 

    foreach ($xml->Show as $Show) { 

      foreach($Show->Time as $Date) { 

       $_date = new DateTime($Date); 

       //if this is a new day, add it to the group 
       if(! isset($group[$_date->format('m.d')])) 
        $group[ $_date->format('m.d') ] = array(); 

       //add the time to the array, organized by month.day 
       $group[ $_date->format('m.d') ][] = $_date->format('H:i'); 
      } 

     } 
     foreach($group as $month => $times) { echo $month . ': <br />'; 
       foreach($times as $time) { echo $time . '<br />'; } 
       } 
    } 

现在它看起来像这样

Array 
(
    [01.05] => Array 
     (
      [0] => 17:30 
      [1] => 20:30 
     ) 

    [01.06] => Array 
     (
      [0] => 20:30 
      [1] => 21:30 
     ) 

    [01.07] => Array 
     (
      [0] => 20:30 
     ) 

) 

嘭!感谢你们!

相关问题