How do I efficiently locate key-value pairs in a multi-dimensional PHP array?

Posted by Kyle Noland on Stack Overflow See other posts from Stack Overflow or by Kyle Noland
Published on 2010-06-10T17:53:07Z Indexed on 2010/06/10 18:02 UTC
Read the original article Hit count: 196

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?

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql