Skip to content
Advertisement

Foreach does not return all values

I have a foreach but it only returns the last value and not all the values, what is the problem?

my array

JavaScript

My code:

JavaScript

Currently it only returns the last value

JavaScript

I need you to return the following (example)

JavaScript

If it is not clear I will try to improve it

Advertisement

Answer

If we consider your data won’t change, and you have 3 entries to get, you can use array_chunk:

JavaScript

But your problem was logical, you don’t change the key in your loop, of course your array will have only the last elements. Reread your code, and try to understand why.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement