我知道这个问题已经得到了回答,但我使用了这个方法,并在代码中扩展了它,这样您就不会只通过uid进行搜索了。我只想把它分享给可能需要这个功能的其他人。
这是我的例子,请记住这是我的第一个答案。我取出了param数组,因为我只需要搜索一个特定的数组,但是您可以很容易地将它添加进来。我想从本质上说不仅仅是通过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;
}
后来,我写了这篇文章,允许我搜索另一个值和关联键。因此,我的第一个示例允许您搜索任何特定关联键中的值,并返回所有匹配项。
第二个示例显示在某个关联键中找到一个值(‘Taylor’)(Name_Name)。和另一个值(True)在另一个关联键(Employeed)中找到,并返回所有匹配项(名称为“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;}
功能的使用$data = array(
array(
'cust_group' => 6,
'price' => 13.21,
'price_qty' => 5
),
array(
'cust_group' => 8,
'price' => 15.25,
'price_qty' => 4
),
array(
'cust_group' => 8,
'price' => 12.75,
'price_qty' => 10
));$findKey = search_revisions($data,'8', 'cust_group', '10', 'price_qty');print_r($findKey);
结果Array ( [0] => 2 )