用分隔符值将php数组拆分为块/段。

时间:2021-07-17 03:32:54

I have written the following code to split the array into three chunks/sections:

我已经编写了以下代码将数组分成三个块/部分:

$data = [
    'SECTION1',
    '1',
    '2',
    '3',
    'SECTION2',
    '4',
    '5',
    'SECTION3',
    '6',
    '7',
    '8',
];


$results = [];


foreach ($data as $value) {
    if(strpos($value, 'SECTION') !== false) {
        array_push($results, []);
    } else {
        array_push($results[count($results)-1], $value);
    }
}

This is the result:

这是由于:

$result = [
    [
        '1',
        '2',
        '3'
    ], [
        '4',
        '5'
    ],[
        '6',
        '7',
        '8'
    ]
];

Is there a more intuitive and readable way to transform this array with a functional-programming approach?

是否有一种更直观、更可读的方法来使用函数编程方法来转换这个数组?

2 个解决方案

#1


0  

Check this one more human readable and managed.

检查这一个人的可读性和管理。

$data = [
    'SECTION1',
    '1',
    '2',
    '3',
    'SECTION2',
    '4',
    '5',
    'SECTION3',
    '6',
    '7',
    '8',
];


$results = [];


foreach ($data as $value) {
    if(strpos($value, 'SECTION') !== false) {
        $results[$value] = array();
    } else {
        $keys = array_keys($results);
        $last = end($keys);
        $results[$last][] = $value;
    }
}
echo '<pre>';
print_r($results);
exit;

Out put will be as

放出来就像。

Array (
    [SECTION1] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [SECTION2] => Array
        (
            [0] => 4
            [1] => 5
        )

    [SECTION3] => Array
        (
            [0] => 6
            [1] => 7
            [2] => 8
        )

)

#2


0  

$data = [
    'SECTION1',
    '1',
    '2',
    '3',
    'SECTION2',
    '4',
    '5',
    'SECTION3',
    '6',
    '7',
    '8',
];


$cnt = 0;
$result = array();
foreach( $data as $arr ) {
    if( strpos($arr, 'SECTION') !== false) {
        $cnt++;
    } else {
        $result[$cnt][] = $arr;
    }   
}


echo "<pre>";   
print_r($result);

//UPDATED

/ /更新

Array
(
    [1] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [2] => Array
        (
            [0] => 4
            [1] => 5
        )

    [3] => Array
        (
            [0] => 6
            [1] => 7
            [2] => 8
        )

)

#1


0  

Check this one more human readable and managed.

检查这一个人的可读性和管理。

$data = [
    'SECTION1',
    '1',
    '2',
    '3',
    'SECTION2',
    '4',
    '5',
    'SECTION3',
    '6',
    '7',
    '8',
];


$results = [];


foreach ($data as $value) {
    if(strpos($value, 'SECTION') !== false) {
        $results[$value] = array();
    } else {
        $keys = array_keys($results);
        $last = end($keys);
        $results[$last][] = $value;
    }
}
echo '<pre>';
print_r($results);
exit;

Out put will be as

放出来就像。

Array (
    [SECTION1] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [SECTION2] => Array
        (
            [0] => 4
            [1] => 5
        )

    [SECTION3] => Array
        (
            [0] => 6
            [1] => 7
            [2] => 8
        )

)

#2


0  

$data = [
    'SECTION1',
    '1',
    '2',
    '3',
    'SECTION2',
    '4',
    '5',
    'SECTION3',
    '6',
    '7',
    '8',
];


$cnt = 0;
$result = array();
foreach( $data as $arr ) {
    if( strpos($arr, 'SECTION') !== false) {
        $cnt++;
    } else {
        $result[$cnt][] = $arr;
    }   
}


echo "<pre>";   
print_r($result);

//UPDATED

/ /更新

Array
(
    [1] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [2] => Array
        (
            [0] => 4
            [1] => 5
        )

    [3] => Array
        (
            [0] => 6
            [1] => 7
            [2] => 8
        )

)