note 91202 deleted from function.array-keys by danbrown
| From: | danbrown@php.net | Date: | Fri, 29 May 2009 17:20:13 +0000 |
| Subject: | note 91202 deleted from function.array-keys by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-156159@lists.php.net to get a copy of this message | ||
Note Submitter: timothy dot burgess dot 87 at gmail dot com
----
Oops sorry... this one is better! Ignore my last post. This function is almost exactly the same as
the "extract_values_by_key" that was made over at the "array_search"
documentation page, but allows for arrays to be returned as well and some weird bug with zeroes is
eliminated (for some reason it was treating "if ($needle_key == $key)" as true when the
key was "0").
For multidimensional arrays, I noticed someone created a function called
"extract_values_by_key" but it didn't work as I wanted it to... so I created a
recursive function of my own that gets the values within a multidimensional array with a specific
key and stores them into an array in case multiple keys match the search.
/* Search for values by key within multidimensional array */
//Timothy Burgess
//05.29.2009
function get_values_by_key($array, $needle_key, &$out = array ()) {
if (is_array($array)) {
foreach ($array as $key => $value) {
get_values_by_key($value, $needle_key, &$out );
if ($key == $needle_key && $key != "0") {
$out[] = $value;
}
}
}
return $out;
}