note 91202 added to function.array-keys
| From: | timothy dot burgess dot 87 at gmail dot com | Date: | Fri, 29 May 2009 16:22:56 +0000 |
| Subject: | note 91202 added to function.array-keys | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-156154@lists.php.net to get a copy of this message | ||
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;
}
----
Server IP: 209.41.74.194
Probable Submitter: 65.198.133.72
----
Manual Page -- http://www.php.net/manual/en/function.array-keys.php
Edit -- https://master.php.net/note/edit/91202
Del: integrated -- https://master.php.net/note/delete/91202/integrated
Del: useless -- https://master.php.net/note/delete/91202/useless
Del: bad code -- https://master.php.net/note/delete/91202/bad+code
Del: spam -- https://master.php.net/note/delete/91202/spam
Del: non-english -- https://master.php.net/note/delete/91202/non-english
Del: in docs -- https://master.php.net/note/delete/91202/in+docs
Del: other reasons-- https://master.php.net/note/delete/91202
Reject -- https://master.php.net/note/reject/91202
Search -- https://master.php.net/manage/user-notes.php