duanjuhuo8772 2011-10-19 07:10
浏览 16
已采纳

搜索多维数组php [复制]

Possible Duplicate:
php. walk up multidimensional array?

I have a multidimensional arrray which contains arrays and objects. I have to search one value in it. How can I do?

view Object
(
[db_table] => views_view
[base_table] => users
[args] => Array
    (
    )

[use_ajax] => 
[result] => Array
    (
    )

[pager] => Array
    (
        [use_pager] => 
        [items_per_page] => 10
        [element] => 0
        [offset] => 0
        [current_page] => 0
    )

[old_view] => Array
    (
        [0] => 
    )

[vid] => 1
[name] => userone
[description] => userone
[tag] => 
[view_php] => 
[is_cacheable] => 0
[display] => Array
    (
        [default] => views_display Object
            (
                [db_table] => views_display
                [vid] => 1
                [id] => default
                [display_title] => Defaults
                [display_plugin] => default
                [position] => 1
                [display_options] => Array
                    (

Like this the array continue. How can I search if one value exists?

  • 写回答

1条回答 默认 最新

  • dongxi7722 2011-10-19 07:42
    关注

    If you only want to find out if a certain value exists and nothing else, this is trivial using recursive iterators:

    $found = false;
    foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $value) {
        if ($value == 'theValueImLookingFor') {
            $found = true;
            break;
        }
    }
    

    It's not much more complex to write this up in a recursive function:

    function recursive_in_array($array, $needle) {
        foreach ($array as $value) {
            $isIterable = is_object($value) || is_array($value);
            if ($value == $needle || ($isIterable && recursive_in_array($value, $needle))) {
                return true;
            }
        }
        return false;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值