使用php在多维数组中搜索数组



我有以下多维数组:

    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)
}

相关内容

  • 没有找到相关文章

最新更新