dongzouhe9734 2015-07-23 17:15
浏览 45

递归查找所有数组值

So I have a big response from an API in a multi-dimensional array, and I need to find all the instances of a certain key->value pair, specifically ['type'] => PhotoField My task would be easy if they were all the same depth, but they vary, so I am using a recursive function to get all the key value pairs of a specific value. I have morphed a standard recursive array searching algorithm for my purposes. But I still have the problem that when it pushes the path of each instance to the $path array, it just merges to the path of the previous instance.

What I really need is for each instance's path to be a subarray within $path.

Here is my function:

function array_searchRecursive($needle, $haystack, $strict=false, $path=array() )
        {
            if(!array_key_exists('elements', $haystack)) {
                return false;
            }
            foreach( $haystack['elements'] as $key => $val ) {
                if( is_array($val) && $subPath = array_searchRecursive($needle, $val, $strict, $path) ) {
                    $path = array_merge($path, array($key), $subPath);
                    return $path;
                } elseif( (!$strict && $val['type'] == $needle) || ($strict && $val['type'] === $needle) ) {
                    $path[] = $val['key'];
                }
            }
            if (!(empty($path))){
                return $path;
            }
            return false;
        }

I call it with:

 array_searchRecursive($resp['form']);

And here is some sample data:

$resp =
    Array
(
    [form] => Array
        (
            [name] => Site Inspection            
            [elements] => Array
                (
                    [0] => Array
                        (
                            [type] => Section
                            [key] => 86d2
                            [elements] => Array
                                (
                                    [0] => Array
                                        (
                                            [type] => ChoiceField
                                            [key] => 450c
                                       )
                                )
                        )

                    [1] => Array
                        (
                            [type] => Section
                            [key] => 6021

                            [elements] => Array
                                (    
                                    [0] => Array
                                        (
                                            [type] => TextField
                                            [key] => c8e5
                                        )

                                    [1] => Array
                                        (
                                            [type] => PhotoField
                                            [key] => 01dd
                                            [label] => Photos of Protective Structure
                                        )    
                                    [2] => Array
                                        (
                                            [type] => PhotoField
                                            [key] => 8e1c
                                            [label] => Photos of Degradation to Protective Structures
                                        )                                        
                                )
                        )
                    [2] => Array
                        (
                            [type] => Section
                            [key] => 9335
                            [elements] => Array
                                (
                                    [0] => Array
                                        (
                                            [type] => TextField
                                            [key] => b614
                                        )
                                    [1] => Array
                                        (
                                            [type] => Repeatable
                                            [key] => 6b00

                                                (
                                                    [0] => Array
                                                        (
                                                            [type] => TextField
                                                            [key] => b646
                                                                                                                    )
                                                    [1] => Array
                                                        (
                                                            [type] => PhotoField
                                                            [key] => 9747
                                                                                                                    )
                                                    )    
                                        )
                                )
                        )
          )

 )

Sincere thanks for any help. It is greatly appreciated.

  • 写回答

1条回答 默认 最新

  • doumeikuan6834 2015-07-23 17:19
    关注

    This should work:

    foreach($resp['form']['elements'] as $v)
    {
        if($v['type'] === 'PhotoField')
        {
            // we found it!
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!