根据特定的内部数组值分割单个数组

时间:2019-03-23 08:49:17

标签: php jquery html arrays columnsorting

$data = 
          [1] => Array
                        (
                            [1] => CITY :
                            [3] => CANADA
                        )

                    [8] => Array
                        (
                            [1] => Customer :
                            [4] => CUS1
                        )
                    [9] => Array
                        (
                            [1] => Customer :
                            [4] => CUS2
                        )
                    [10] => Array
                        (
                            [1] => Customer :
                            [4] => CUS3
                        )
                   .............

                    [17] => Array
                        (
                            [1] => CITY :
                            [3] => FLORIDA
                        )

                    [18] => Array
                        (
                            [1] => Customer :
                            [3] => C2
                        )
               )


我正在尝试根据值将数组拆分为不同的数组。这是我的数组。我需要根据内部值“ CITY:”将单个数组拆分为多个 计算所有“ CITY:”,并将数组从第一个“ CITY”分割为下一个“ CITY”和下一个...。

结果: 我想根据如下值分割数组

[1] => Array
                        (
                            [1] => CITY :
                            [3] => CANADA
                        )

                    [8] => Array
                        (
                            [1] => Customer :
                            [4] => CUS1
                        )
                    [9] => Array
                        (
                            [1] => Customer :
                            [4] => CUS2
                        )
                    [10] => Array
                        (
                            [1] => Customer :
                            [4] => CUS3
                        )
          )

 [2] => Array
                        (
                            [1] => CITY :
                            [3] => FLORIDA
                        )

                    [18] => Array
                        (
                            [1] => Customer :
                            [3] => C2
                        )


我不知道如何分割

1 个答案:

答案 0 :(得分:1)

这应该对您有用:

<?php

$data = array(
    array(
        'CITY :',
        'CANADA'
    ),
    array(
        'Customer :',
        'CUS1'
    ),
    array(
        'Customer :',
        'CUS2'
    ),
    array(
        'Customer :',
        'CUS3'
    ),
    array(
        'CITY :',
        'FLORIDA'
    ),
    array(
        'Customer :',
        'C2'
    ),
);

$result = array();
$firstRun = true;
foreach($data as $item) {
    if(array_values($item)[0] == 'CITY :') {
        if(!$firstRun) {
            $result[] = $splittedArray;
        } else {
            $firstRun = false;
        }

        $splittedArray = array();
    }

    $splittedArray[] = $item;
}
// add last array, as it will not pass at the finish through the first logic of the loop
$result[] = $splittedArray;

print_r($result);