【问题标题】:Splitting up the value of an XML Element into two, then group them将 XML 元素的值分成两部分,然后将它们分组
【发布时间】:2015-10-23 01:32:49
【问题描述】:

我需要一些帮助。看起来很简单,但我无法正确完成。 这是我的 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');
        }
    }

?>

我省去了其余的代码,因为我实际上已经废弃了它 - 反正它是错误的,而且我是一个菜鸟: 将 $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

如您所见,我希望将 $hours 按 $day 分组,或者更确切地说,跳过/删除每个重复的 $day。 我更喜欢用 PHP 来做这件事,而不是 XSL。

是的,我用谷歌搜索了几个小时,因为没有得到它而感到非常愚蠢。感谢您的帮助!

【问题讨论】:

  • 我提供了一个答案,澄清了一些日期操作的东西,但我不确定我是否完全理解你想要做什么。

标签: php arrays xml foreach


【解决方案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'
    ),
);

等等。

【讨论】:

    【解决方案2】:

    我使用了 Chris 的建议,稍作改动,效果很好,谢谢!

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

    砰!谢谢各位!

    【讨论】:

      【解决方案3】:

      创建一个数组来存储值。

      $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 />';
          }
      }
      

      【讨论】:

      • 感谢您的帮助,但这仍然显示: 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
      • 是的,它把它说出来是因为我把我吐出来的作为一个例子。试试print_r($group)。你可以遍历数组并用它做任何你想做的事情。
      • 终于成功了!请参阅下面的评论。非常感谢!
      猜你喜欢
      • 1970-01-01
      • 2022-01-22
      • 2014-10-12
      • 1970-01-01
      • 1970-01-01
      • 2021-08-04
      • 2020-04-07
      • 2020-07-27
      • 2020-09-21
      相关资源
      最近更新 更多