How do I efficiently locate key-value pairs in a multi-dimensional PHP array?
- by Kyle Noland
I have an array in PHP as a result of the following query to a Wordpress database:
SELECT * FROM wp_postmeta WHERE post_id = :id
I am returned a multidimensional array that looks like this:
Array ( [0] => Array ( [meta_id] => 380 [post_id] => 72 [meta_key] => _edit_last [meta_value] => 1 )
... etc.
What is the best way to find a particular key-value pair in this array?
For instance, how would I located the row where [meta_key] = event_name so that I can extract that same row's [meta_value] value into a PHP variable?
I realize I could turn this into many individual MySQL queries. Does anyone have an opinion of the efficiency of doing 10 SQL queries in a row rather than searching the array 10 times? I would think since the array is in memory, that will be the fastest method to find the values I need.
Alternatively, is there a better way to query the database from the beginning so that my result set is formatted in a way that is easier to search?