note 94748 added to function.array-keys
| From: | jojor at gmx dot net | Date: | Sun, 22 Nov 2009 23:43:55 +0000 |
| Subject: | note 94748 added to function.array-keys | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-163176@lists.php.net to get a copy of this message | ||
function array_keys_md($array, $md_field = "id") {
$keys = array();
if (is_array($array) && count($array) > 0) {
foreach ($array as $a) {
if (isset($a[$md_field])) {
array_push($keys, $a[$md_field]);
} else
{
return false;
}
}
return $keys;
}
return false;
}
In the past i often needed the particular fields which are actually are right ids... for example two
dimensional assoc array returned by databases...
function array_keys_md($array, $md_field = "id") {
$keys = array();
if (is_array($array) && count($array) > 0) {
foreach ($array as $a) {
if (isset($a[$md_field])) {
array_push($keys, $a[$md_field]);
} else
{
return false;
}
}
return $keys;
}
return false;
}
an input array could be:
$array = array(
0 -> array(
"id" = 6,
"animal" = "cat"
),
1 -> array(
"id" = 9,
"animal" = "dog"
)
);
$id_keys = array_keys_md($array, "id"); // 6, 9
$animals_as_key = array_keys_md($array, "animal"); //"cat", "dog"
----
Server IP: 212.124.37.9
Probable Submitter: 85.16.24.110
----
Manual Page -- http://www.php.net/manual/en/function.array-keys.php
Edit -- https://master.php.net/note/edit/94748
Del: integrated -- https://master.php.net/note/delete/94748/integrated
Del: useless -- https://master.php.net/note/delete/94748/useless
Del: bad code -- https://master.php.net/note/delete/94748/bad+code
Del: spam -- https://master.php.net/note/delete/94748/spam
Del: non-english -- https://master.php.net/note/delete/94748/non-english
Del: in docs -- https://master.php.net/note/delete/94748/in+docs
Del: other reasons-- https://master.php.net/note/delete/94748
Reject -- https://master.php.net/note/reject/94748
Search -- https://master.php.net/manage/user-notes.php