【问题标题】:Search in multidimensional Stdclass PHP在多维 Stdclass PHP 中搜索
【发布时间】:2019-08-22 21:23:36
【问题描述】:

我想在 PHP 中搜索特定对象。例如 [module] => play 在 StdClass 对象中,如何在类本身中搜索值?

你可以在下面找到整个数组:

            [flow] => stdClass Object
                (
                    [data] => stdClass Object
                        (
                            [action] => start
                        )

                    [module] => record_call
                    [children] => stdClass Object
                        (
                            [_] => stdClass Object
                                (
                                    [data] => stdClass Object
                                        (
                                            [id] => 7696364e90822fdcba4feee30574a7e2
                                            [endless_playback] => 
                                            [terminators] => Array
                                                (
                                                    [0] => 1
                                                )

                                        )

                                    [module] => play
                                    [children] => stdClass Object
                                        (
                                            [_] => stdClass Object
….

我想得到如下输出:

                                    [data] => stdClass Object
                                        (
                                            [id] => 7696364e90822fdcba4feee30574a7e2
                                            [endless_playback] => 
                                            [terminators] => Array
                                                (
                                                    [0] => 1
                                                )

                                        )

                                    [module] => play
                                    [children] => stdClass Object
                                        (
                                            [_] => stdClass Object
….

有人可以帮我吗?提前谢谢!

【问题讨论】:

  • 您的数据是否必须采用stdClass 对象的形式?如果您通过json_decode() 接收数据,则可以将true 作为第二个参数传入以接收关联数组,在这种情况下,您可以轻松执行递归数组搜索。

标签: php arrays object


【解决方案1】:

这里有几个更简单的选择。

第一个是如果您通过json_decode() 调用接收此数据,请添加true 作为第二个参数:

$decoded = json_decode($data, true);

第二个是利用上面的优势和json_encode()的使用,把这个转换成简单的关联数组结构:

$decoded = json_decode(json_encode($data), true);

使用您的简单数组结构,现在可以递归遍历数组以找到所需的匹配键/值对:

function findModule($data, $module_name) {
    if(!is_array($data)) return null;

    if(array_key_exists('module', $data)) {
        if($data['module'] === $module_name) {
            return $data;
        }
    }

    foreach($data as $key=>$val) {
        $result = findModule($val, $module_name);
        if(!is_null($result)) {
            return $result;
        }
    }

    return null;
}

// Will either return the array of array('data'=>..., 'module'=>'play', 'children': ...) or null if no matching module found.
$module = findModule($decoded, 'play');

【讨论】:

    猜你喜欢
    • 2016-05-11
    • 2016-09-19
    • 2017-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 2011-05-20
    • 2017-07-23
    相关资源
    最近更新 更多