Skip to content
Advertisement

Array data manipulations PHP

I get from my DB data in format like this:

JavaScript

Each of array inside has a key: “id”. It mostly look values from 1 to 3. I would like to create a new array based on this “ids” that would look like this:

JavaScript

I`ve been trying to do it like this:

JavaScript

And that would work, but it doesn`t look good in my opinion, beacuse what if the id change to some big number.

Advertisement

Answer

Yeah, having separate arrays for each ID is not a good idea. How about:

JavaScript

Simple, and straight to the point.

Haven’t tested it. Next time try to use var_export() to export the data you put in your question. That way we can actually use it, without having to rewrite it completely.

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