搜索多维数组一个键,然后改变与PHP的值 [英] Searching for a key in a multidimensional array then changing a value with PHP

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

问题描述

我有一个多维数组,看起来像这样

I have a multidimensional array that looks like this

[0] => Array
    (
        [recordId] => 5
        [leaf] => 1
        [children] => Array
            (
                [0] => Array
                    (
                        [recordId] => 6
                        [leaf] => 1
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [recordId] => 7
                                        [leaf] => 1
                                    )
                            )
                    )
                [1] => Array
                    (
                        [recordId] => 8
                        [leaf] => 1
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [recordId] => 9
                                        [leaf] => 1
                                    )
                                [1] => Array
                                    (
                                        [recordId] => 10
                                        [leaf] => 1
                                    )
                            )
                    )
            )
    )

每个节点都有一个叶键时,默认值为TRUE,并有一个孩子数组,如果有进一步的节点了。

Each node has a 'leaf' key that is TRUE by default and has a 'children' array if there are further nodes down.

我需要设置叶键值为FALSE如果是包含在节点中的孩子阵列。这样,只有最后的节点有叶= TRUE标志。

I need to set the 'leaf' key value to FALSE if there is a 'children' array contained in the node. That way only final nodes have the leaf = TRUE designation.

我尝试过寻找,但找不到code做什么,我需要,我不能换我的头周围,我认为是必要的递归函数。

I've tried searching but can't find code to do what I need and I can't wrap my head around the recursive function that I believe is needed.

任何想法我怎么能在PHP中做到这一点?

Any ideas how I could accomplish this in PHP?

感谢您的帮助。

推荐答案

在理论上这应该工作:

function findChild(&$array){
     foreach($array as &$arr){
            if(isset($arr['children'])){
                  $arr['leaf'] = 0; //there are children
                  findChild($arr['children']);
            }
            else {
                  $arr['leaf'] = 1; //there are no children
            }
     }
}

下面是一个工作演示: HTTP://$c$cpad.org/AnYiRpES

Here is a working demo: http://codepad.org/AnYiRpES

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

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