$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.
2
Answers
You can do that using ${} to create dynamic variables like:
And also check for the presence of (‘type’ => ‘separator’) in a foreach, so you can increase the $counter.
The following code:
prints: