0
$one = array('start' => '2012-10-12', 'stop' => '2012-11-04', 'over' => false);
$two = array('start' => '2012-10-29', 'stop' => '2012-11-14', 'over' => true);
$three = array('start' => '2012-11-12', 'stop' => '2012-12-07', 'over' => false);

$array = array($one, $two, $three);

这告诉我:

Array
(
    [0] => Array
        (
            [start] => 2012-10-12
            [stop] => 2012-11-04
            [over] => 
        )

    [1] => Array
        (
            [start] => 2012-10-29
            [stop] => 2012-11-14
            [over] => 1
        )

    [2] => Array
        (
            [start] => 2012-11-12
            [stop] => 2012-12-07
            [over] => 
        )

)

我想要拆分这些日期的函数。如果 in array is over = true那么这个日期应该分割其他日期。

对于这个例子,这应该返回:

Array
(
    [0] => Array                  //this is changed
        (
            [start] => 2012-10-12
            [stop] => 2012-10-28
            [over] => 
        )

    [1] => Array                   //this is ok
        (
            [start] => 2012-10-29
            [stop] => 2012-11-14
            [over] => 1
        )

    [2] => Array                  //this is changed
        (
            [start] => 2012-11-15
            [stop] => 2012-12-07
            [over] => 
        )

)

第二个例子:

$one = array('start' => '2012-10-12', 'stop' => '2012-12-04', 'over' => false);
$two = array('start' => '2012-10-29', 'stop' => '2012-11-14', 'over' => true);


$second = array($one, $two);

这还给我:

Array
(
    [0] => Array
        (
            [start] => 2012-10-12
            [stop] => 2012-12-04
            [over] => 
        )

    [1] => Array
        (
            [start] => 2012-10-29
            [stop] => 2012-11-14
            [over] => 1
        )

)

我想收到:

Array
(
    [0] => Array                  //this is changed - old [0] date
        (
            [start] => 2012-10-12
            [stop] => 2012-10-28
            [over] => 
        )

    [1] => Array                  //this is ok
        (
            [start] => 2012-10-29
            [stop] => 2012-11-14
            [over] => 1
        )
    [2] => Array                  //this is new - from old [0] date - next part
        (
            [start] => 2012-11-15
            [stop] => 2012-12-04
            [over] => 
        )
)

我怎样才能用 PHP 做到这一点?

4

1 回答 1

0

试试这个。

foreach($arr as $key=>$val)
{
   if(isset($arr[$key+1]) && $arr[$key+1]['over'] == true)
       $arr[$key]['stop'] = date('Y-m-d', strtotime($arr[$key+1]['start'] .' -1 day'));
  if(isset($arr[$key-1]) && $arr[$key-1]['over'] == true)
       $arr[$key]['start'] = date('Y-m-d', strtotime($arr[$key-1]['stop'] .' +1 day'));
}

你可以在这里找到代码

更新:

 $arr1 = $arr;
 foreach($arr as $key=>$val)
{
   if(isset($arr[$key+1]) && $arr[$key+1]['over'] == true)
   {
       $arr[$key]['stop'] = date('Y-m-d', strtotime($arr[$key+1]['start'] .' -1 day'));
       $arr[$key]['over'] = true;
   }
   if(isset($arr[$key-1]) && $arr[$key-1]['over'] == true)
   {
       $arr[$key]['start'] = date('Y-m-d', strtotime($arr[$key-1]['stop'] .' +1 day'));
       $arr[$key]['over'] = true;
   }
 }

foreach($arr1 as $key=>$val)
{
   $arr[$key]['over'] = $arr1[$key]['over'];
}

您在此处更新的 代码

于 2012-10-26T12:36:59.877 回答