0

I have some array to go through foreach. Inside, I need to generate an array of quantity.

It still brings me back in both 0-5.

The output should look like this:

Output

$a = array(['id' => 1,'quantity' => 5,'input' => 'one'],
            ['id' => 2,'quantity' => 4,'input' => 'two'] );
foreach ($a as $b) {
        for ($x = 0; $x <= $b['quantity']; $x++) {
            $count[$x] = $x;
        }
        dump($b['quantity']);
        dump($count);
}

1 Answer 1

1

Well it's hard to tell what you want to achieve, but I think your current described problem is, that you are overwriting the same array in your second loop and so it still contains 5 elements from the first loop.

Add this at the top of your foreach:

foreach ($a as $b) {
    $count = [];
    ...

This will reset the $count array on every iteration. This might bring new problems, but that's not easy to tell by the supplied information..

Sign up to request clarification or add additional context in comments.

1 Comment

Thx. It's very simple. xD

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.