Skip to content
Advertisement

PHP – array_push to create a multidimensional array

I am trying to create an array which will have two key/value pairs for each user. I would like to add their user ID as well as their name.

Currently I have this:

JavaScript

This gives me the following:

JavaScript

However, to make it easier to read and work with, I would prefer that it shows user_id and user_name as the keys, rather than just the index. I believe this is what’s called a multidimensional array and would look more like this:

JavaScript

My question is how would I build an array like this within my foreach loop?

Advertisement

Answer

You just need to create the new array and add it to the $userArray, I use [] instead of array_push() though…

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