I am trying to loop through an array of block components, that each can have n number of nested components (profile or avatar).
Now, what I want to do is to show these blocks x number of times, where x is the number of data from a payload array:
$payload['users'] = [
['name' => 'Oliver'],
['name' => 'John']
];
So since the above payload users length is 2, this should be rendered:
- block #1
-- profile
-- avatar
- block #2
-- profile
-- avatar
I am trying to render the above the same way by using a nested foreach loop. See the below code:
$payload['users'] = [
['name' => 'Oliver'],
['name' => 'John']
];
$schema = [
"id" => 1,
"name" => "Users",
"components" => [
[
"key" => "0",
"name" => "block",
"components" => [
[
"key" => "1",
"name" => "profile"
],
[
"key" => "2",
"name" => "avatar"
]
],
]
],
];
$toPush = [];
foreach ($schema['components'] as $key => $value) {
foreach ($value['components'] as $no => $component) {
$iterator = $payload['users'];
for ($x = 0; $x < count($iterator); $x ++) {
$copy = $component;
$copy['item'] = $iterator[$x];
$copy['key'] = $copy['key'] . '-' . $x;
$toPush[] = $copy;
}
$schema['components'][$key]['components'] = $toPush;
}
}
print_r($toPush);
The problem is that the above prints it out like this:
- block #1
-- profile
-- profile
- block #2
-- avatar
-- avatar
I have created an 3v4l of this, which can be found here.
How can I achieve my desired scenario?
For reference I am using the Laravel framework.
Desired output
Also available as an 3v4l here.
[
"components" => [
[
"key" => "1",
"name" => "profile",
"item" => [
"name" => "Oliver"
]
],
[
"key" => "2",
"name" => "avatar",
"item" => [
"name" => "Oliver"
]
],
[
"key" => "3",
"name" => "profile",
"item" => [
"name" => "John"
]
],
[
"key" => "4",
"name" => "avatar",
"item" => [
"name" => "John"
]
]
],
];
payload.users) is custom data, that my users can specify. What I want to do, is that for the count ofpayload.users, I want to render the nested components undercomponents.name = block. But they should be rendered "pair-wise" and not sequentially.