如何在PHP中获取这样的数组?

时间:2013-06-22 07:26:54

标签: php arrays

我有一个像这样的事件数组:

 Array (

     [0] = Array(
                  [EventId] => 596
                  [Event] => test
                  [ResourceId] => 6
                  [StartDate] =>  2013-06-21 10:00:00
                  [FinishDate]  2013-06-21 10:30:00                   
                )
     [1] = Array(
                  [EventId] => 598
                  [Event] => another test
                  [ResourceId] => 6
                  [StartDate] =>  2013-06-21 15:00:00
                  [FinishDate]  2013-06-21 16:30:00                   
                )
     [2] = Array(
                  [EventId] => 599
                  [Event] => test
                  [ResourceId] => 8
                  [StartDate] =>  2013-06-21 10:00:00
                  [FinishDate]  2013-06-21 10:30:00                   
                )
      [3] = Array(
                  [EventId] => 601
                  [Event] => another test
                  [ResourceId] => 10
                  [StartDate] =>  2013-06-21 15:00:00
                  [FinishDate]  2013-06-21 16:30:00                   
                )
      [4] = Array(
                  [EventId] => 602
                  [Event] => new event
                  [ResourceId] => 10
                  [StartDate] =>  2013-06-22 10:00:00
                  [FinishDate]  2013-06-22 12:30:00                   
                )
      [5] = Array(
                  [EventId] => 604
                  [Event] => try new event
                  [ResourceId] => 6
                  [StartDate] =>  2013-06-22 10:00:00
                  [FinishDate]  2013-06-22 12:30:00                   
                )
       )

我希望事件数组的结果数组如下:

   Array (

     [0] = Array(
                  [EventId] => 596,599
                  [Event] => test
                  [ResourceId] => 6,8
                  [StartDate] =>  2013-06-21 10:00:00
                  [FinishDate]  2013-06-21 10:30:00                   
                )
     [1] = Array(
                  [EventId] => 598,601
                  [Event] => another test
                  [ResourceId] => 6,10
                  [StartDate] =>  2013-06-21 15:00:00
                  [FinishDate]  2013-06-21 16:30:00                   
                )         
     [2] = Array(
                  [EventId] => 602
                  [Event] => new event
                  [ResourceId] => 10
                  [StartDate] =>  2013-06-22 10:00:00
                  [FinishDate]  2013-06-22 12:30:00                   
                )
     [3] = Array(
                  [EventId] => 604
                  [Event] => try new event
                  [ResourceId] => 6
                  [StartDate] =>  2013-06-22 10:00:00
                  [FinishDate]  2013-06-22 12:30:00                   
                )
       )

我正在研究PHP。但我不知道如何迭代数组以匹配其2个值并添加到新数组中。

我需要匹配StartDate和EndDate。如果两者都相同,那么我需要连接资源ID并合并该记录。

我尝试使用for循环,但它没有用。

我试过这个:

  for($i=0;$i<count($event);$i++)
  {
if(($event[$i]["StartDate"]==$event[$i+1]["StartDate"]) && ($event[$i]["FinishDate"]==$event[$i+1]["FinishDate"]))
     {
     echo "yes"."<br>"; // Add in new array
     }
     else
     {
    echo "no"."<br>";
     }      
  }

有人可以帮助我吗?

提前致谢。

2 个答案:

答案 0 :(得分:0)

多维数组!您将需要嵌套for循环:

for($i=0;$i<4;$i++)
{
for($j=0;$j<4;$j++)
{
$var = $your_array[i][j];
echo $var;//do whatever you want to do with the variable here
}
}

答案 1 :(得分:0)

如下:

$out = array();
$c = count($event);
for ($i = 0; $i < $c; $i++) {
    $out[$i] = $event[$i];
    for ($j = $i+1; $j < $c; $j++) {
        if ($event[$i]["StartDate"] == $event[$j]["StartDate"] && 
            $event[$i]["FinishDate"] == $event[$j]["FinishDate"]) {
            $out[$i]["EventId"] .= ", " . $event[$j]["EventId"];
            // do the same with other indices
        }
    }
}