用键分割动态数组。

时间:2014-05-13 11:44:55

标签: php arrays

我有一个包含相同键的数组。我想按密钥名称拆分它。我想通过TrackingId拆分它。例如,下面的数组由3个跟踪id组成,我想制作3个不同的数组.Array如下 -

Array
(
    [0] => Array
        (
            [0] => TrackingId
            [1] => 22173753
        )

    [1] => Array
        (
            [0] => TrackingIdServ
            [1] => 47
        )

    [2] => Array
        (
            [0] => MobileNumber
            [1] => ***********
       )
    [3] => Array
    (
        [0] => TransSubmitter
        [1] => TPASUA2
     )

    [4] => Array
        (
            [0] => TrackingId
            [1] => 22153392
        )

    [5] => Array
        (
            [0] => TrackingIdServ
            [1] => 47
        )

    [6] => Array
        (
            [0] => MobileNumber
            [1] => ************     
        }

    [7] => Array
        (
            [0] => TrackingId
            [1] => 1184031
        )

    68] => Array
        (
            [0] => TrackingIdServ
            [1] => 47
        )

    [9] => Array
        (
            [0] => MobileNumber
            [1] => ************
        )
)   

1 个答案:

答案 0 :(得分:0)

我是这样做的。

$i=0;
foreach($result as $key){
    $i++;
    if($key[0]=="TrackingId"){
        $loc[]=$i;
        $start[]=$i-1;
        $end[]=$i-1;
    }
    $lastKey=$i;
}

$arrLength=count($loc);

$chunks=array();

for($i=0;$i<$arrLength;$i++){

    $j=$i+1;
    $v=$end[$j]-$lastKey;

    if($start[$i]==0){
        $chunks[]=array_slice($result,$start[$i],$end[$j]);
    }else if($end[$j]=="") {
        $chunks[]=array_slice($result,$start[$i],$lastKey);
    }else{
        $chunks[]=array_slice($result,$start[$i],$v);
    }

}

它返回我想要的结果如下。

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => TrackingId
                    [1] => 22173753
                )

            [1] => Array
                (
                    [0] => TrackingIdServ
                    [1] => 47
                )

            [2] => Array
                (
                    [0] => MobileNumber
                    [1] => XXXXXXXXXXX
                )
            [3] => Array
                (
                    [0] => TransSubmitter
                    [1] => TPASUA2
                 )  
        )       
        [1] => Array
        (
            [0] => Array
                (
                    [0] => TrackingId
                    [1] => 1184031
                )

            [1] => Array
                (
                    [0] => TrackingIdServ
                    [1] => 47
                )

            [2] => Array
                (
                    [0] => MobileNumber
                    [1] => XXXXXXXXXXXX
                )
        )
          [2] => Array
        (
            [0] => Array
                (
                    [0] => TrackingId
                    [1] => 22153392
                )

            [1] => Array
                (
                    [0] => TrackingIdServ
                    [1] => 47
                )

            [2] => Array
                (
                    [0] => MobileNumber
                    [1] => XXXXXXXXXXX
                )

            )
)