First of all, I’ve got a result array from a db query that i’m spitting out into a html list via a foreach. Secondly I have got a multi-dimensional array with related data in. The relationship is defined in both data sources by “entry_id”.
I want to query the multi-dimensional array on each iteration of the foreach to check if there is a matching entry_id, and if so i want to pull the page title out to add into my html list.
The multi-dimensional array looks like this:
array(4) {
[0]=> array(2) {
["entry_id"]=> string(1) "1"
["title"]=> string(4) "Page Title 1"
}
[1]=> array(2) {
["entry_id"]=> string(1) "2"
["title"]=> string(5) "Page Title 2"
}
[2]=> array(2) {
["entry_id"]=> string(1) "3"
["title"]=> string(8) "Page Title 3"
}
[3]=> array(2) {
["entry_id"]=> string(1) "4"
["title"]=> string(5) "Page Title 4" }
}
So firstly, how would you query that multi-dimensional array and get the page title out that you need on that iteration, bearing in mind the sub-arrays have identical keys?
Secondly, what is the most efficient method of doing this, I should also mention that the array could get very large in theory.
Transform the array so that you have the structure
i.e.
Then getting the title is just accessing the array with the value of the
entry_id.If you can, build the array like this from the beginning, if not, you have to iterate over it and create this structure.