PHP多维数组搜索值

我有一个数组,我想搜索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

我试着做循环,但我想要一个更快的执行代码。

739349 次浏览
function searchForId($id, $array) {
foreach ($array as $key => $val) {
if ($val['uid'] === $id) {
return $key;
}
}
return null;
}

这是可行的。你应该这样称呼它:

$id = searchForId('100', $userdb);

重要的是要知道,如果你使用===操作符比较类型必须完全相同,在这个例子中,你必须搜索string或直接使用==代替===

基于< a href = " https://stackoverflow.com/a/24527099/819364 " > angoru < / >的答案。在PHP (>= 5.5.0)的后续版本中,您可以使用一行程序。

$key = array_search('100', array_column($userdb, 'uid'));

下面是文档:http://php.net/manual/en/function.array-column.php

我知道这个问题已经回答了,但我在代码中使用了这个问题并对其进行了扩展,这样就不需要只根据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;
}

后来,我写了这个,以允许我搜索另一个值和关联键。因此,我的第一个示例允许您在任何特定的关联键中搜索值,并返回所有匹配的值。

第二个例子告诉你一个值('Taylor')在某个关联键(first_name) 另一个值(true)在另一个关联键(hired)中找到,并返回所有匹配(名字为'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 )

在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);

我必须使用un函数来查找数组中的每个元素。所以我修改了Jakub truneeek的函数如下:

function search_in_array_r($needle, $array) {
$found = array();
foreach ($array as $key => $val) {
if ($val[1] == $needle) {
array_push($found, $val[1]);
}
}
if (count($found) != 0)
return $found;
else
return null;
}

我修改了下面描述函数函数的的例子之一。函数searchItemsByKey通过$key从多维数组(N层)返回所有值。也许,它会对某人有用。例子:

 $arr = array(
'XXX'=>array(
'YYY'=> array(
'AAA'=> array(
'keyN' =>'value1'
)
),
'ZZZ'=> array(
'BBB'=> array(
'keyN' => 'value2'
)
)
//.....
)
);




$result = searchItemsByKey($arr,'keyN');


print '<pre>';
print_r($result);
print '<pre>';
// OUTPUT
Array
(
[0] => value1
[1] => value2
)

函数代码:

function searchItemsByKey($array, $key)
{
$results = array();


if (is_array($array))
{
if (isset($array[$key]) && key($array)==$key)
$results[] = $array[$key];


foreach ($array as $sub_array)
$results = array_merge($results, searchItemsByKey($sub_array, $key));
}


return  $results;
}

你可以使用这个函数; https://github.com/serhatozles/ArrayAdvancedSearch < / p >

<?php
include('ArraySearch.php');


$query = "a='Example World' and b>='2'";


$Array = array(
'a' => array('d' => '2'),
array('a' => 'Example World','b' => '2'),
array('c' => '3'), array('d' => '4'),
);


$Result = ArraySearch($Array,$query,1);


echo '<pre>';
print_r($Result);
echo '</pre>';


// Output:
// Array
// (
//    [0] => Array
//        (
//            [a] => Example World
//            [b] => 2
//        )
//
// )

如果你使用的是(PHP 5 >= 5.5.0),你不需要自己写函数来实现这个功能,只需要写这一行就可以了。

如果你只想要一个结果:

$key = array_search(40489, array_column($userdb, 'uid'));

对于多个结果

$keys = array_keys(array_column($userdb, 'uid'), 40489);

如果你有一个关联数组,就像注释中指出的那样,你可以用:

$keys = array_keys(array_combine(array_keys($userdb), array_column($userdb, 'uid')),40489);

如果你正在使用PHP <5.5.0,你可以使用这个补丁,谢谢ramsey!

更新:我一直在做一些简单的基准测试,多重结果表单似乎是最快的,甚至比Jakub自定义函数还要快!

/**
* searches a simple as well as multi dimension array
* @param type $needle
* @param type $haystack
* @return boolean
*/
public static function in_array_multi($needle, $haystack){
$needle = trim($needle);
if(!is_array($haystack))
return False;


foreach($haystack as $key=>$value){
if(is_array($value)){
if(self::in_array_multi($needle, $value))
return True;
else
self::in_array_multi($needle, $value);
}
else
if(trim($value) === trim($needle)){//visibility fix//
error_log("$value === $needle setting visibility to 1 hidden");
return True;
}
}


return False;
}

虽然这是一个老问题,也有一个公认的答案,但我想对公认的答案提出一个改变。首先,我同意这个公认的答案是正确的。

function searchArrayKeyVal($sKey, $id, $array) {
foreach ($array as $key => $val) {
if ($val[$sKey] == $id) {
return $key;
}
}
return false;
}

将预设的“uid”替换为函数中的一个参数,因此现在调用下面的代码意味着您可以跨多个数组类型使用一个函数。零钱很小,但能带来细微的差别。

    // Array Data Of Users
$userdb = array (
array ('uid' => '100','name' => 'Sandra Shush','url' => 'urlof100' ),
array ('uid' => '5465','name' => 'Stefanie Mcmohn','url' => 'urlof100' ),
array ('uid' => '40489','name' => 'Michael','url' => 'urlof40489' ),
);


// Obtain The Key Of The Array
$arrayKey = searchArrayKeyVal("uid", '100', $userdb);
if ($arrayKey!==false) {
echo "Search Result: ", $userdb[$arrayKey]['name'];
} else {
echo "Search Result can not be found";
}

PHP Fiddle Example .

for( $i =0; $i < sizeof($allUsers); $i++)
{
$NEEDLE1='firstname';
$NEEDLE2='emailAddress';
$sterm='Tofind';
if(isset($allUsers[$i][$NEEDLE1]) && isset($allUsers[$i][$NEEDLE2])
{
$Fname= $allUsers[$i][$NEEDLE1];
$Lname= $allUsers[$i][$NEEDLE2];


$pos1 = stripos($Fname, $sterm);
$pos2=stripos($Lname, $sterm);//not case sensitive


if($pos1 !== false ||$pos2 !== false)
{$resultsMatched[] =$allUsers[$i];}
else
{   continue;}
}


}
Print_r($resultsMatched); //will give array for matched values even partially matched

在上述代码的帮助下,可以从2D数组中的任何列中找到任何(部分匹配)数据,因此可以根据需要找到用户id。

$a = ['x' => ['eee', 'ccc'], 'b' => ['zzz']];


$found = null;
$search = 'eee';


array_walk($a, function ($k, $v) use ($search, &$found) {
if (in_array($search, $k)) {
$found = $v;
}
});


var_dump($found);

扩展创建的@mayhem函数,这个例子将更像是一个“模糊”搜索,如果你只是想匹配搜索字符串的部分(大多数):

 function searchArrayKeyVal($sKey, $id, $array) {
foreach ($array as $key => $val) {
if (strpos(strtolower($val[$sKey]), strtolower(trim($id))) !== false) {
return $key;
}
}
return false;
}

例如,数组中的值是Welcome to New York!而你只想要第一个“纽约”

看起来array_filter将适合这个解决方案…

$userdb=Array
(
(0) => Array
(
(uid) => '100',
(name) => 'Sandra Shush',
(url) => 'urlof100'
),


(1) => Array
(
(uid) => '5465',
(name) => 'Stefanie Mcmohn',
(pic_square) => 'urlof100'
),


(2) => Array
(
(uid) => '40489',
(name) => 'Michael',
(pic_square) => 'urlof40489'
)
);

PHP代码

<?php
$search = 5465;
$found = array_filter($userdb,function($v,$k) use ($search){
return $v['uid'] == $search;
},ARRAY_FILTER_USE_BOTH); // With latest PHP third parameter is optional.. Available Values:- ARRAY_FILTER_USE_BOTH OR ARRAY_FILTER_USE_KEY


$values= print_r(array_values($found));
$keys =  print_r(array_keys($found));

试试这个

<?php
function recursive_array_search($needle,$haystack) {
foreach($haystack as $key=>$value) {
$current_key=$key;
if($needle===$value OR (is_array($value) &&
recursive_array_search($needle,$value) !== false)) {
return $current_key;
}
}
return false;
}
?>

在PHP的后续版本(>= 5.5.0)中,您可以使用以下一行代码:

$key = array_search('100', array_column($userdb, 'uid'));

我想检查以下数组$arr中是否有'abc'存在于子数组中

$arr = array(
array(
'title' => 'abc'
)
);

然后我可以用这个

$res = array_search('abc', array_column($arr, 'title'));
if($res == ''){
echo 'exists';
} else {
echo 'notExists';
}

我认为这是最简单的定义方式

这里有一句同样的句子,

$pic_square = $userdb[array_search($uid,array_column($userdb, 'uid'))]['pic_square'];

如果问题,即。

$a = [
[
"_id" => "5a96933414d48831a41901f2",
"discount_amount" => 3.29,
"discount_id" => "5a92656a14d488570c2c44a2",
],
[
"_id" => "5a9790fd14d48879cf16a9e8",
"discount_amount" => 4.53,
"discount_id" => "5a9265b914d488548513b122",
],
[
"_id" => "5a98083614d488191304b6c3",
"discount_amount" => 15.24,
"discount_id" => "5a92806a14d48858ff5c2ec3",
],
[
"_id" => "5a982a4914d48824721eafe3",
"discount_amount" => 45.74,
"discount_id" => "5a928ce414d488609e73b443",
],
[
"_id" => "5a982a4914d48824721eafe55",
"discount_amount" => 10.26,
"discount_id" => "5a928ce414d488609e73b443",
],
];

答:

function searchForId($id, $array) {
$did=0;
$dia=0;
foreach ($array as $key => $val) {
if ($val['discount_id'] === $id) {
$dia +=$val['discount_amount'];
$did++;
}
}
if($dia != '') {
echo $dia;
var_dump($did);
}
return null;
};
print_r(searchForId('5a928ce414d488609e73b443',$a));

只是分享,也许可以像这样。

if( ! function_exists('arraySearchMulti')){
function arraySearchMulti($search,$key,$array,$returnKey=false)
{
foreach ($array as $k => $val) {
if (isset($val[$key])) {
if ((string)$val[$key] == (string)$search) {
return ($returnKey ? $k : $val);
}
}else{
return (is_array($val) ? arraySearchMulti($search,$key,$val,$returnKey) : null);
}
}
return null;
}}

你可以通过两个函数的组合来做到这一点,array_search &array_column

$search_value = '5465';
$search_key   = 'uid';
$user = array_search($search_value, array_column($userdb, $search_key));


print_r($userdb[$user]);

5465是要搜索的用户ID, uid是包含用户ID的键,userdb美元是问题中定义的数组。

引用:

array_search on php.net

array_column on php.net

还没有人使用过array_reduce,所以我想添加这个方法…

$find_by_uid = '100';
$is_in_array = array_reduce($userdb, function($carry, $user) use ($find_by_uid){
return $carry ? $carry : $user['uid'] === $find_by_uid;
});
// Returns true

与array_search()相比,可以更好地控制'search'逻辑。

注意,我在这里使用了严格的等式,但您可以选择不同的比较逻辑。$carry意味着比较需要为真一次,最终结果将为真。

这里有一个更好的解决方案,如果您从数据库或多维数组中提取数据

多维数组的例子:

$records = array(
array(
'id' => 2135,
'first_name' => 'John',
'last_name' => 'Doe',
),
array(
'id' => 3245,
'first_name' => 'Sally',
'last_name' => 'Smith',
),
array(
'id' => 5342,
'first_name' => 'Jane',
'last_name' => 'Jones',
),
array(
'id' => 5623,
'first_name' => 'Peter',
'last_name' => 'Doe',
)
);




function search_user_by_name($name, $array) {
foreach ($array as $keys) {
foreach ($keys as $key => $_user_record) {
if ($_user_record == $name) {
return [$key => $_user_record];//Return and array of user
}
}
}
return null;
}

调用函数:

$results = search_user_by_name('John', $records);
print_r($results);
         

输出:Array ( [first_name] => John )

我正在寻找类似于MySQL像%term%的功能。根据本页的答案。我能够从文件中搜索JSON数组。

user_list.json看起来像下面的例子:

{
"user-23456": {
"name": "John Doe",
"age": "20",
"email": "doe@sample.com",
"user_id": "23456"
},
"user-09876": {
"name": "Ronojoy Adams",
"age": "35",
"email": "joy@sample.com",
"user_id": "09876"
},
"user-34890": {
"name": "Will Artkin",
"age": "16",
"email": "will@sample.com",
"user_id": "34890"
},
}


/*
*search_key_like
*/


function search_key_like($value, $key, $array) {
$results=array();
$keyword = preg_quote($value, '~');
foreach ($array as $k => $val) {
//if name a is spell John and keyword is sent as joh or JOH it will return null
//to fix the issue convert the string into lowercase and uppercase
$data=array($val[$key],strtolower($val[$key]),strtoupper($val[$key]));
if (preg_grep('~' . $keyword . '~', $data)) {
array_push($results,$val[$key]);
}
}
return $results;
}

使用===提取JSON文件===

 $user_list_json='./user_list.json';
if(file_exists($user_list_json) && file_get_contents($user_list_json)){
$file_json_data=file_get_contents($user_list_json);
        

$json_array_data=json_decode($file_json_data,true);
        

$user_name_like = search_key_like('ron', 'name', $json_array_data);
   

print "<pre>".print_r($user_name_like,true);
}