-1

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

 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>";
     }      
  }

谁能帮帮我吗 ?

提前致谢。

4

2 回答 2

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
}
}
于 2013-06-22T07:30:39.673 回答
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
        }
    }
}
于 2013-06-22T10:03:32.977 回答