oliverbj
oliverbj

Reputation: 6052

PHP - Nested foreach loop in pair-wise

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"
            ]
        ]
    ],
];

Upvotes: 0

Views: 132

Answers (1)

berend
berend

Reputation: 631

This logic may be of some help to you.

$toPush = [];
$count = 0;
foreach ($schema['components'] as $value) {
    foreach ($value['components'] as $key => $component) {
        foreach ($payload['users'] as $idx => $user) {
            $toPush['components'][$count]['key'] = $count;
            $toPush['components'][$count]['name'] = $value['components'][$idx]['name'];
            $toPush['components'][$count]['item'] = $payload['users'][$key];
            $count++;
        }
    }
}

demo

Upvotes: 1

Related Questions