Home > Software design >  separate an array into multiple arrays with a separator kay value pair php
separate an array into multiple arrays with a separator kay value pair php

Time:11-01


$test = [
0 => [
'type' => 'separator',
'data' => 'separator1'
],
1 => [
'type' => 'image',
'data' => 'image1url'
],
3 => [
'type' => 'separator',
'data' => 'separator2'
],
4 => [
'type' => 'video',
'data' => 'video1url'
],
5 => [
'type' => 'image',
'data' => 'image3url'
],
];

I have an array that has multiple arrays in it. I need to separate the array into multiple arrays like the example below.


$array1 = [

0 => [
'type' => 'separator',
'data' => 'separator1'
],
1 => [
'type' => 'image',
'data' => 'image1url'
],
];

$array2 = [
0 => [
'type' => 'separator',
'data' => 'separator2'
],
1 => [
'type' => 'video',
'data' => 'video1url'
],
2 => [
'type' => 'image',
'data' => 'image3url'
],
];

Is there any way to do such thing? Please consider that the item-count between separators can be changed.

I tried to do it using a foreach but didn't succeed.

CodePudding user response:

The following code:

$test = [
0 => [
'type' => 'separator',
'data' => 'separator1'
],
1 => [
'type' => 'image',
'data' => 'image1url'
],
3 => [
'type' => 'separator',
'data' => 'separator2'
],
4 => [
'type' => 'video',
'data' => 'video1url'
],
5 => [
'type' => 'image',
'data' => 'image3url'
],
6 => [
'type' => 'separator',
'data' => 'separator3'
],
7 => [
'type' => 'video',
'data' => 'video3url'
],
8 => [
'type' => 'image',
'data' => 'image5url'
],
];

$aaResult = [];
$count=-1;
foreach ($test as $subarray) {
    if ($subarray['type']=='separator') {
        $count  ;
        $aaResult[$count] = [];
        $aaResult[$count][] = $subarray;
    }
    else {
        $aaResult[$count][] = $subarray;
    }
}

// if you want separate arrays ($array1, $array2,..) and not $aaResult[0], $aaResult[1],...
$varNamePrefix='array';
for ($i=0;$i<count($aaResult);$i  ) {
    $varName = $varNamePrefix.($i 1);
    $$varName = $aaResult[$i];
}
print_r($array1);
print_r($array2);
print_r($array3);

prints:

Array
(
    [0] => Array
        (
            [type] => separator
            [data] => separator1
        )

    [1] => Array
        (
            [type] => image
            [data] => image1url
        )

)
Array
(
    [0] => Array
        (
            [type] => separator
            [data] => separator2
        )

    [1] => Array
        (
            [type] => video
            [data] => video1url
        )

    [2] => Array
        (
            [type] => image
            [data] => image3url
        )

)
Array
(
    [0] => Array
        (
            [type] => separator
            [data] => separator3
        )

    [1] => Array
        (
            [type] => video
            [data] => video3url
        )

    [2] => Array
        (
            [type] => image
            [data] => image5url
        )

)

CodePudding user response:

You can do that using ${} to create dynamic variables like:

 ${'array' . $counter} = 'your array';

And also check for the presence of ('type' => 'separator') in a foreach, so you can increase the $counter.

  • Related