Skip to content
Advertisement

Adding values to multidimensional array PHP without overwritting last added value

I’m having trouble with adding values to an associative multidimensional array and echoing them out. The last value added is the one being echoed out. I tried playing with $j next to $dreams[$name] but then it only echoes out the first letter. My code is as follows:

JavaScript

Advertisement

Answer

You need

JavaScript

This creates a new array entry within $dreams[$name] for each entered value. Otherwise yes you’re overwriting $dreams[$name] every time with a different value.

And then to output, you’ll need two loops, one for the names and one for the dreams – it should be a mirror of the input process – because effectively you’re doing the process in reverse, with the same data structure:

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