基于值将数组拆分为多个数组 - PHP

时间:2014-03-14 09:27:11

标签: php arrays

到目前为止我尝试过:我有一个如下所示的数组,我想要做的是,根据值BEGIN_DAY创建多个数组:

Array
(
    [0] => Array
        (
            [0] => BEGIN_DAY
            [1] => 15
        )

    [1] => Array
        (
            [0] => 20130701
            [1] => 4
            [2] => 4
            [3] => 3060
            [4] => 1
        )

    [2] => Array
        (
            [0] => 20130702
            [1] => 270
            [2] => 757
            [3] => 13812810
            [4] => 4
        )

    [3] => Array
        (
            [0] => 20130703
            [1] => 5
            [2] => 123
            [3] => 3894971
            [4] => 2
        )

    [4] => Array
        (
            [0] => 20130704
            [1] => 290
            [2] => 478
            [3] => 5119617
            [4] => 1
        )
    [5] => Array
        (
            [0] => END_DAY
            [1] => 15
        )
    [6] => Array
        (
            [0] => BEGIN_DAY
            [1] => 16
        )
     [7] => Array
        (
            [0] => 20130704
            [1] => 290
            [2] => 478
            [3] => 5119617
            [4] => 1
        )

依旧...... 我希望将此数组拆分为多个不同的数组,当它以BEGIN_DAY开头时,它应该如下所示: 第一个:

 Array
(
    [0] => Array
        (
            [0] => BEGIN_DAY
            [1] => 15
        )

    [1] => Array
        (
            [0] => 20130701
            [1] => 4
            [2] => 4
            [3] => 3060
            [4] => 1
        )

    [2] => Array
        (
            [0] => 20130702
            [1] => 270
            [2] => 757
            [3] => 13812810
            [4] => 4
        )

    [3] => Array
        (
            [0] => 20130703
            [1] => 5
            [2] => 123
            [3] => 3894971
            [4] => 2
        )

    [4] => Array
        (
            [0] => 20130704
            [1] => 290
            [2] => 478
            [3] => 5119617
            [4] => 1
        )
    [5] => Array
        (
            [0] => END_DAY
            [1] => 15
        )

第二个:

    [0] => Array
        (
            [0] => BEGIN_DAY
            [1] => 16
        )
    [1] => Array
        (
            [0] => 20130704
            [1] => 290
            [2] => 478
            [3] => 5119617
            [4] => 1
        )

到目前为止我尝试过:

$days=array();
$split_by='BEGIN_DAY';
foreach ($day_all as $key => $value) {
    if($day_all[$key][0]==$split_by){
         $days=array_slice($day_all, 0,$split_by);
  }
}
 var_dump($days);

非常赞赏!

2 个答案:

答案 0 :(得分:1)

$sk = "first";
foreach ($array as $key=>$value)
{
    if(in_array(BEGIN_DAY, $value)&&$key!=0)
    {
        $sk = "second";
    }
    $result[$sk][] = $value;
}
echo "<pre>";
print_r($result);
$first = $result['first'];
$second = $result['second'];

答案 1 :(得分:1)

这样的事情:

$yourArray = ...; //is your array as described above
$finalArray = [];
$tempArray = [];
for($idx = 0; idx<$yourArray .length; idx++){
  $tempArray = $yourArray[$idx]

  if($yourArray[$idx][0] == 'BEGIN_DAY'){
    $finalArray[] = $tempArray;
    $tempArray = [];
  }
}

最终数组将包含数组。每次找到BEGIN_DAY时,数组都将插入到finalArray中,并创建一个新数组。 要改进代码,也要检查单元格是否存在,以避免索引异常。