The purpose of this question is to find the best way to print data out of PHP multidimensional arrays.
How can you complete the following procedure below?
I have the following arrays
array1['id']['title']
and
array2['id']['tags'][]
The arrays have been generated by the function pg_fetch_array
.
This allows you refer to each value of element by its name or by its key.
Procedure to get the titles for questions and their tags
I would like to do the following
- First loop
- print the title from
array1[$question_id]
- print all tags from
array2[$question_id][]
for the given question_id
- print the title from
- Second loop
- do the same as in 1.1 for the next
question_id
in the list - do the same as in 1.2 for the next
question_id
in the list ...
- do the same as in 1.1 for the next
- Continue this for all $question_ids in the list
I have used various methods unsuccessfully to complete the procedure
- to create a multidimensional array such that I can iterate though all items in a singe -foreach: merge_unique is not enough here. Other merges also remove one column which I do not want.
- to solve the problem with the two given arrays by
while
andforeach
-sentences: I get 9 iterations for 3 questions as I have a foreach -clause inside a while -loop