我有这个多维数组。我需要搜索它,只返回与"鼻涕虫"的值匹配的键。我知道还有其他关于搜索多维数组的线程,但是我没有真正理解到足以应用于我的情况。非常感谢您的帮助!
所以我需要一个这样的函数:myfunction($products,'breville-one-touch-tea-maker-BTM800XL');
// returns 1
数组:
$products = array (
1 => array(
'name' => 'The Breville One-Touch Tea Maker',
'slug' => 'breville-one-touch-tea-maker-BTM800XL',
'shortname' => 'The One-Touch Tea Maker',
'listprice' => '299.99',
'price' => '249.99',
'rating' => '9.5',
'reviews' => '81',
'buyurl' => 'http://www.amazon.com/The-Breville-One-Touch-Tea-Maker/dp/B003LNOPSG',
'videoref1' => 'xNb-FOTJY1c',
'videoref2' => 'WAyk-O2B6F8',
'image' => '812BpgHhjBML.jpg',
'related1' => '2',
'related2' => '3',
'related3' => '4',
'bestbuy' => '1',
'quote' => '',
'quoteautor' => 'K. Martino',
),
2 => array(
'name' => 'Breville Variable-Temperature Kettle BKE820XL',
'slug' => 'breville-variable-temperature-kettle-BKE820XL',
'shortname' => 'Variable Temperature Kettle',
'listprice' => '199.99',
'price' => '129.99',
'rating' => '9',
'reviews' => '78',
'buyurl' => 'http://www.amazon.com/Breville-BKE820XL-Variable-Temperature-1-8-Liter-Kettle/dp/B001DYERBK',
'videoref1' => 'oyZWBD83xeE',
'image' => '41y2B8jSKmwL.jpg',
'related1' => '3',
'related2' => '4',
'related3' => '5',
'bestbuy' => '1',
'quote' => '',
'quoteautor' => '',
),
);
另一种可能的解决方案是基于array_search()
函数。需要使用PHP 5.5.0或更高版本。
$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'
)
);
$key = array_search(40489, array_column($userdb, 'uid'));
echo ("The key is: ".$key);
//This will output- The key is: 2
函数' array_search() '有两个参数。第一个是要搜索的值。第二个是函数应该搜索的位置。函数"array_column()"获取键为"uid"的元素的值。总结所以你可以这样用:array_search('breville-one-touch-tea-maker-BTM800XL', array_column($products, 'slug'));
$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'
)
);
$key = array_search(40489, array_column($userdb, 'uid'));
echo ("The key is: ".$key);
//This will output- The key is: 2
array_search('breville-one-touch-tea-maker-BTM800XL', array_column($products, 'slug'));
或者,如果你喜欢:
// define function
function array_search_multidim($array, $column, $key){
return (array_search($key, array_column($array, $column)));
}
// use it
array_search_multidim($products, 'slug', 'breville-one-touch-tea-maker-BTM800XL');
原始示例(通过xfoxaway)可以在DOCS中找到。array_column()
页面
由于Vael的评论,我很好奇,所以我做了一个简单的测试来衡量使用array_search
的方法和在接受的答案上提出的方法的性能。
我创建了一个包含1000个数组的数组,结构是这样的(所有数据都是随机的):
[
{
"_id": "57fe684fb22a07039b3f196c",
"index": 0,
"guid": "98dd3515-3f1e-4b89-8bb9-103b0d67e613",
"isActive": true,
"balance": "$2,372.04",
"picture": "http://placehold.it/32x32",
"age": 21,
"eyeColor": "blue",
"name": "Green",
"company": "MIXERS"
},...
]
我运行搜索测试100次,为name字段搜索不同的值,然后计算以毫秒为单位的平均时间。下面是一个示例:
结果表明,在此答案上提出的方法需要约2E-7才能找到值,而接受的答案方法需要约8E-7。
就像我之前说的,对于使用这种大小的数组的应用程序来说,这两种情况都是可以接受的。如果大小增加很多,比如1M个元素,那么这个小差异也会增加。更新二
我已经为基于array_walk_recursive
的方法添加了一个测试,这是在这里的一些答案中提到的。得到的结果是正确的。如果我们把注意力集中在表现上,它比其他参加考试的人要差一点。在测试中,您可以看到它比基于array_search
的方法慢了大约10倍。同样,对于大多数应用程序来说,这不是一个非常相关的区别。
更新三世
感谢@mickmackusa指出了这个方法的几个限制:
- 此方法将在关联键上失败。
- 这个方法只适用于索引子数组(从0开始,键连续升序)。
更新III注释
- 不考虑性能:你可以使用array_combine和array_keys &array_column可以在一行代码中克服这个限制:
$product_search_index =
array_search( 'breville-one-touch-tea-maker-BTM800XL', array_filter( array_combine( array_keys($products), array_column( $products, 'slug' ) ) ) );
非常简单:
function myfunction($products, $field, $value)
{
foreach($products as $key => $product)
{
if ( $product[$field] === $value )
return $key;
}
return false;
}
这个类方法可以在数组中搜索多个条件:
class Stdlib_Array
{
public static function multiSearch(array $array, array $pairs)
{
$found = array();
foreach ($array as $aKey => $aVal) {
$coincidences = 0;
foreach ($pairs as $pKey => $pVal) {
if (array_key_exists($pKey, $aVal) && $aVal[$pKey] == $pVal) {
$coincidences++;
}
}
if ($coincidences == count($pairs)) {
$found[$aKey] = $aVal;
}
}
return $found;
}
}
// Example:
$data = array(
array('foo' => 'test4', 'bar' => 'baz'),
array('foo' => 'test', 'bar' => 'baz'),
array('foo' => 'test1', 'bar' => 'baz3'),
array('foo' => 'test', 'bar' => 'baz'),
array('foo' => 'test', 'bar' => 'baz4'),
array('foo' => 'test4', 'bar' => 'baz1'),
array('foo' => 'test', 'bar' => 'baz1'),
array('foo' => 'test3', 'bar' => 'baz2'),
array('foo' => 'test', 'bar' => 'baz'),
array('foo' => 'test', 'bar' => 'baz'),
array('foo' => 'test4', 'bar' => 'baz1')
);
$result = Stdlib_Array::multiSearch($data, array('foo' => 'test4', 'bar' => 'baz1'));
var_dump($result);
会产生:
array(2) {
[5]=>
array(2) {
["foo"]=>
string(5) "test4"
["bar"]=>
string(4) "baz1"
}
[10]=>
array(2) {
["foo"]=>
string(5) "test4"
["bar"]=>
string(4) "baz1"
}
}
使用此函数:
function searchThroughArray($search,array $lists){
try{
foreach ($lists as $key => $value) {
if(is_array($value)){
array_walk_recursive($value, function($v, $k) use($search ,$key,$value,&$val){
if(strpos($v, $search) !== false ) $val[$key]=$value;
});
}else{
if(strpos($value, $search) !== false ) $val[$key]=$value;
}
}
return $val;
}catch (Exception $e) {
return false;
}
}
和call函数
print_r(searchThroughArray('breville-one-touch-tea-maker-BTM800XL',$products));
function search($array, $key, $value)
{
$results = array();
if (is_array($array))
{
if (isset($array[$key]) && $array[$key] == $value)
$results[] = $array;
foreach ($array as $subarray)
$results = array_merge($results, search($subarray, $key, $value));
}
return $results;
}
我想这样做,其中$products
是在最开始的问题中给出的实际数组。
print_r(
array_search("breville-variable-temperature-kettle-BKE820XL",
array_map(function($product){return $product["slug"];},$products))
);
试试这个
function recursive_array_search($needle,$haystack) {
foreach($haystack as $key=>$value) {
$current_key=$key;
if($needle==$value['uid'] OR (is_array($value) && recursive_array_search($needle,$value) !== false)) {
return $current_key;
}
}
return false;
}
对于下一个访问者:使用递归数组遍历;它访问多维数组中的每个"叶子"。这里有一些灵感:
function getMDArrayValueByKey($a, $k) {
$r = [];
array_walk_recursive ($a,
function ($item, $key) use ($k, &$r) {if ($key == $k) $r[] = $item;}
);
return $r;
}
您可以将数组转换为JSON并作为字符串搜索,然后返回找到的对象,因此无论嵌套有多深,它都会很快找到它:
function findObjectByKeyValue($array, $key, $value){
$object = [];
$string = json_encode($array);
$foundPosition = strpos($string, '"' . $key . '":"' . $value . '"');
if( $foundPosition ){
$prevBracketPos = strrpos(substr($string, 0, $foundPosition), '{');
if( $prevBracketPos ){
$nextBracketPos = strpos($string, '}', $foundPosition);
if( $nextBracketPos ){
$brackets = 0;
while( strpos(substr($string, $foundPosition, $nextBracketPos - $foundPosition), '{') &&
substr_count(substr($string, $foundPosition, $nextBracketPos - $foundPosition), '{') > $brackets
){
$lenToAdd = strlen(substr($string, $foundPosition, $nextBracketPos - $foundPosition + 1));
$nextBracketPos = strpos($string, '}', $foundPosition + $lenToAdd);
$brackets++;
}
$substr = substr($string, $prevBracketPos, $nextBracketPos - $prevBracketPos + 1);
// Confirm it's wrapped with brackets before we decode
if( substr($substr, 0, 1) === '{' && substr($substr, -1, 1) === '}' ){
$object = json_decode($substr, true);
}
}
}
}
return $object;
}
的例子:
$arr = [{
"items":
{
"1": [
{
"id": "621eaf06062cd",
"nestedItem":
{
"id": "123",
"nestedItem":
{
"id": "456",
"nestedItem":
{
"id": "789"
}
}
}
}],
"2": [
{
"id": "621eb58de7364",
}],
"3": [
{
"id": "62226910716af",
}]
}
}];
echo findObjectByKeyValue($arr, 'id', '123');
/* {
"id": "123",
"nestedItem":
{
"id": "456",
"nestedItem":
{
"id": "789"
}
}
} /*
echo findObjectByKeyValue($arr, 'id', '621eaf06062cd');
/* {
"id": "621eaf06062cd",
"nestedItem":
{
"id": "123",
"nestedItem":
{
"id": "456",
"nestedItem":
{
"id": "789"
}
}
}
} */
уоu可能遇到的唯一问题是如果数组中有括号
在关联数组上运行良好:
public function getKeyByParamValue(string $param, mixed $value, array $array): mixed
{
$keys = $this->getKeysByParamValue($param, $value, $array);
return empty($keys) ? null : $keys[0];
}
public function getKeysByParamValue(string $param, mixed $value, array $array): ?array
{
return array_keys(
array_combine(
array_keys($array),
array_column($array, $param)
),
$value
);
}