如何在php中一起搜索id到数组和俱乐部

时间:2014-11-19 12:29:41

标签: php arrays array-merge

好吧,我严重陷入困境,我一直在通过从数组中查找id来进行数组合并。我想从数组中找到SubjectID,如果找到则将数组合并并计算实际小时数到时间(计算两者之间的时间)。我的数组代码.. 实际数组:

Array
(
    [eventlist] => Array
        (
            [0] => Array
                (
                    [allDay] => false
                    [id] => {83AE8778-A604-46F1-94BE-A045F2A9B548}
                    [SubjectID] => 2
                    [RoomID] => 2
                    [FromTime] => 00:30
                    [ToTime] => 01:45
                    [PlottingType] => Research
                )

            [1] => Array
                (
                    [allDay] => false
                    [id] => {2B1F7B32-6D44-4700-8C50-F030B94D41F6}
                    [RoomID] => 2
                    [FromTime] => 00:00
                    [ToTime] => 00:45
                    [PlottingType] => Research
                )

         [2] => Array
                (
                    [allDay] => false
                    [id] => {83AE8778-A604-46F1-94BE-A045F2A9B548}
                    [SubjectID] => 3
                    [RoomID] => 2
                    [FromTime] => 00:30
                    [ToTime] => 01:45
                    [PlottingType] => Admin
                )

        )

)

expected output:
Array
(
    [eventlist] => Array
        (
            [0] => Array
                (
                    [Time] => 01:15
                    [PlottingType] => Research
                )

         [2] => Array
                (
                    [Time] => 00:15
                    [PlottingType] => Admin
                )

        )

)

请建议我适当的解决方案..

2 个答案:

答案 0 :(得分:0)

嗯,你应该预先通过所有这些

$array; // Your array
$new_array = array(); // Empty new array
$new_array['eventlist'] = array();
foreach($array['eventlist'] as $inner) {

    if(isset($inner['SubjectID']) {
        // Work out your maths for time difference and put it into $interval
        $fromtime = new DateTime($inner['FromTime']);
        $totime   = new DateTime($inner['ToTime']);

        $interval = $fromtime->diff($totime);
        $interval = $interval->format('%H:%i');

        $new_inner = array(
            "Time" => $interval,
            "PlottingType" => $inner['PlottingType']
        );
        array_push($new_array['eventlist'], $new_inner);
    }

}

未经测试。这应该与PHP 5.4+

一起使用

答案 1 :(得分:0)

这是我对这个问题的尝试。 它保留了你的例子中的键。

$newarr = array();
$map = array();

foreach ($arr['eventlist'] as $key=>$val) 
{
  if (!isset($map[$val['PlottingType']]))
    $map[$val['PlottingType']] = $key;

  $fromTime = new DateTime($val['FromTime']);
  $toTime = new DateTime($val['ToTime']);
  $diff = $fromTime->diff($toTime);

  $type = $val['PlottingType'];

  if (!isset($newarr[$map[$val['PlottingType']]])) 
  {
    $newarr[$map[$val['PlottingType']]] = array(
      'time'=>$diff->format("%H:%i"),
      'PlottingType'=>$type
    );
  }
  else 
  {
    $oldtime = new DateTime($newarr[$map[$val['PlottingType']]]['time']);
    $newtime = $oldtime->add($diff);
    $newarr[$map[$val['PlottingType']]]['time'] = $newtime->format("H:i");
  }
}