搜索多维数组PHP [英] Search a multidimensional array php

查看:91
本文介绍了搜索多维数组PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
   PHP。走到多维数组?

我有一个包含数组和对象多维arrray。我在它来搜索一个值。我该怎么办?

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?

推荐答案

如果你只是想看看某个值存在,没有别的,这是微不足道的使用的递归迭代的:

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

这篇关于搜索多维数组PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆