我有以下多维数组:
Array
(
[entity_id] => 5740
[parent_id] => 5739
[label] => Sports
[name] => sports
[icon] => http://localhost/magento/media/catalog/category/
[level] => 3
[tab_id] => Array
(
[0] =>
)
[color] => #555555
[coordinate] => Array
(
[0] =>
)
[amount] => 100000
[children] => Array
(
[0] => Array
(
[entity_id] => 5754
[parent_id] => 5740
[label] => Badminton
[name] => badminton
[icon] => http://localhost/magento/media/catalog/category/
[level] => 4
[tab_id] => Array
(
[0] => 354
)
[color] => #DACC5C
[coordinate] => Array
(
[0] => 12.9539974,77.6309395
)
[amount] => 75000
[children] => Array
(
[0] => Array
(
[entity_id] => 5756
[parent_id] => 5754
[label] => Tournaments
[name] => tournaments
[icon] => http://localhost/magento/media/catalog/category/
[level] => 5
[tab_id] => Array
(
[0] => 354
)
[color] => #8DCD55
[coordinate] => Array
(
[0] => 12.9539974,77.6309395
)
[amount] => 42187.5
)
)
)
如何对此执行数组搜索以获得所需的子数组。我有一个动态值,我想在此基础上进行搜索。
假设我想搜索
[label] => Badminton
,我怎样才能获得该数组?
试试这个:
function search_recursive(array $children, $key, $value){
foreach($children as $child){
if(isset($child[$key]) && $child[$key] === $value){
return $child;
}
if(isset($child['children']) && is_array($child['children'])){
$found = search_recursive($child['children'], $key, $value);
if($found){
return $found;
}
}
}
return false;
}
$children = [$array];
$result = search_recursive($children, 'label', 'Badminton');
您必须递归搜索:
function array_search_recursive($haystack, $keyToBeSearched, $valueToMatch)
{
if (array_key_exists($keyToBeSearched, $haystack)
&& $haystack[$keyToBeSearched] == $valueToMatch
) {
return $haystack;
}
foreach ($haystack as $key => $value) {
if (is_array($value)) {
return array_search_recursive($value, $keyToBeSearched, $valueToMatch);
}
}
return false;
}
@vikas.badola ..我想没有这样的内置功能可以满足您的要求。但你可以编写一个简单的函数,例如:
function search_array($arr, $field)
{
foreach ($arr as $data)
{
if ($arr['label'] == $field)
return $arr;
}
}
$badminton = search_array($data['children'], 'badminton');
希望这会有所帮助!!!
与示例相同的递归实现:
function recursive_search($arr, $k, $v)
{
if (!is_array($arr)) {
return null;
}
if (array_key_exists($k, $arr) && $arr[$k] == $v) {
return $arr;
}
foreach ($arr as $key => $val) {
$found = recursive_search($val, $k, $v);
if (!is_null($found)) {
return $found;
}
}
return null;
}
$arr = array(
'a' => array(
'd' => 1,
'b' => array(
'c' => 2,
'd' => 2,
)
)
);
var_dump(recursive_search($arr, 'd', 0));
var_dump(recursive_search($arr, 'd', 1));
var_dump(recursive_search($arr, 'd', 2));
打印
NULL
array(2) {
["d"]=>
int(1)
["b"]=>
array(2) {
["c"]=>
int(2)
["d"]=>
int(2)
}
}
array(2) {
["c"]=>
int(2)
["d"]=>
int(2)
}
我的代码片段在很大程度上类似于@Stalinko的未解释的通用代码片段,但更适合所问问题的上下文,并且不会进行不必要的检查。
由于第一级不是列表,但所有后续子级都是列表,因此将输入数组作为包含数组的唯一元素传递到递归函数中。 这将为每个级别提供一致的结构,以实现更简单的递归。
假设匹配项的子子数组没有值,则在找到符合条件的匹配项后取消设置子数组。
在遍历级别时,如果返回值不为空,则将假定它是一个填充数组(标识为“真实”值),并且该值应在调用堆栈中一路向上传递。
代码:(演示)
function labelSearch(array $array, string $label): array {
foreach ($array as $item) {
if ($item['label'] === $label) {
unset($item['children']);
return $item;
}
if (isset($item['children'])) {
$result = labelSearch($item['children'], $label);
if ($result) {
return $result;
}
}
}
return null;
}
var_export(labelSearch([$array], 'Badminton'));