我有一个数组,我想搜索`uid'并获得数组的键。
假设我们有以下的二维数组。
$userdb = array(
array(
'uid' => '100',
'name' => 'Sandra Shush',
'pic_square' => 'urlof100'
),
array(
'uid' => '5465',
'name' => 'Stefanie Mcmohn',
'pic_square' => 'urlof100'
),
array(
'uid' => '40489',
'name' => 'Michael',
'pic_square' => 'urlof40489'
)
);
函数调用search_by_uid(100)
(第一个用户的uid)应该返回0
。
函数调用search_by_uid(40489)
应返回2
。
我试着做了循环,但我想要一个更快的执行代码。
function searchForId($id, $array) {
foreach ($array as $key => $val) {
if ($val['uid'] === $id) {
return $key;
}
}
return null;
}
这样就可以了。你应该这样称呼它。
$id = searchForId('100', $userdb);
重要的是要知道,如果你使用====
运算符,比较的类型必须完全相同,在这个例子中,你必须搜索string
或者直接使用==
代替===
。
根据angoru的回答。在后来的PHP版本中(>=5.5.0
),你可以使用one-liner。
$key = array_search('100', array_column($userdb, 'uid'));
基于Jakub'的优秀答案,这里有一个更通用的搜索,将允许指定密钥(不仅仅是uid)。
function searcharray($value, $key, $array) {
foreach ($array as $k => $val) {
if ($val[$key] == $value) {
return $k;
}
}
return null;
}
使用方法。$results = searcharray('searchvalue', searchkey, $array);
。
我知道这个问题已经有人回答过了,但是我使用了这个方法,并在我的代码中进行了一些扩展,这样你就不用只通过uid来搜索了。我只是想把它分享给其他可能需要这种功能的人。
这是我的例子,请记住这是我的第一个答案。我去掉了参数数组,因为我只需要搜索一个特定的数组,但你可以很容易地把它加进去。我想在本质上通过不止是uid来搜索。
另外,在我的情况下,可能会有多个键被返回,因为通过其他字段搜索的结果可能不是唯一的。
/**
* @param array multidimensional
* @param string value to search for, ie a specific field name like name_first
* @param string associative key to find it in, ie field_name
*
* @return array keys.
*/
function search_revisions($dataArray, $search_value, $key_to_search) {
// This function will search the revisions for a certain value
// related to the associative key you are looking for.
$keys = array();
foreach ($dataArray as $key => $cur_value) {
if ($cur_value[$key_to_search] == $search_value) {
$keys[] = $key;
}
}
return $keys;
}
后来,我最后写了这个,让我可以搜索另一个值和关联键。所以我的第一个例子允许你在任何特定的关联键中搜索一个值,并返回所有的匹配值。
第二个例子告诉你在某个关联键(first_name)中找到一个值('Taylor'),***另一个值(true)在另一个关联键(employed)中找到,并返回所有的匹配(名字为'Taylor'且被雇用的键)。
/**
* @param array multidimensional
* @param string $search_value The value to search for, ie a specific 'Taylor'
* @param string $key_to_search The associative key to find it in, ie first_name
* @param string $other_matching_key The associative key to find in the matches for employed
* @param string $other_matching_value The value to find in that matching associative key, ie true
*
* @return array keys, ie all the people with the first name 'Taylor' that are employed.
*/
function search_revisions($dataArray, $search_value, $key_to_search, $other_matching_value = null, $other_matching_key = null) {
// This function will search the revisions for a certain value
// related to the associative key you are looking for.
$keys = array();
foreach ($dataArray as $key => $cur_value) {
if ($cur_value[$key_to_search] == $search_value) {
if (isset($other_matching_key) && isset($other_matching_value)) {
if ($cur_value[$other_matching_key] == $other_matching_value) {
$keys[] = $key;
}
} else {
// I must keep in mind that some searches may have multiple
// matches and others would not, so leave it open with no continues.
$keys[] = $key;
}
}
}
return $keys;
}
函数的使用
结果
Array ( [0] => 2 )