根据其他同级值更新多维数组值

时间:2019-04-03 15:38:11

标签: php arrays recursion

我有一个数组,最多可以有n个元素。

    (
[children] => Array
    (
        [0] => Array
            (
                [id] => nhbs0123620cf897
                [title] => Introduction
                [children] => Array
                    (
                        [0] => Array
                            (
                                [id] => bylff0a76617c8
                                [title] => Courent3
                                [children] => Array
                                    (
                                    )
                            )

                        [1] => Array
                            (
                                [id] => xavs26efa2f51eb
                                [title] => Chapter
                                [children] => Array
                                    (
                                    )
                            )

                        [2] => Array
                            (
                                [id] => iezkd241d9d90
                                [title] => external
                                [children] => Array
                                    (
                                    )
                            )

                        [3] => Array
                            (
                                [id] => gmzh439c4f50
                                [title] => audio
                                [children] => Array
                                    (
                                    )
                            )

                        [4] => Array
                            (
                                [id] => niugd4e18b0
                                [title] => url
                                [children] => Array
                                    (
                                    )
                            )

                        [5] => Array
                            (
                                [id] => unpgdb1b7694
                                [title] => new
                                [children] => Array
                                    (
                                    )

                            )

                        [6] => Array
                            (
                                [id] => ssvc2025c0c8a
                                [title] => simple
                                [children] => Array
                                    (
                                    [0] => Array
                                        (
                                            [id] => ssvc2025c0c
                                            [title] => later
                                            [children] => Array
                                                (
                                                )
                                        )
                                    )
                            )

                    )

            )

        [1] => Array
            (
                [id] => rwtae5d9482
                [title] => Summary
                [children] => Array
                    (
                    [0] => Array
                            (
                                [id] => ssvc2025c0
                                [title] => later
                                [children] => Array
                                    (
                                    )
                            )
                    )

            )
        [2] => Array
            (
                [id] => rwtae5d9482709
                [title] => Course
                [children] => Array
                    (
                    )

            )

    )

  )

在这里,我想根据数组中每个元素的ID更新标题值。

我尝试过的

1用过array_walk_recursive,在这里我可以更新数据,但更新时无法检查id值。

 array_walk_recursive(array, function(&$value, $key) {
      // not able to check if id == 'something' .this is what i need 
        if ($key == 'title') {
            $value = 'updated data';
        }
    });

第二次尝试每个循环,但无法保存数组索引以获取实际数组

 function myfun($array,&$out){
    foreach($array['children'] as $key=>$val){
        if(!empty($val['children'])){
            $out['children'][$key]['title'] = $val['title']; // this is not right key 
            $this->myfun($val,$out['children']);
        }else{
            $out['children'][$key]['title'] = $val['title'];  // this is not right key
        }
    }
}

这里也不能返回$ out数组中的数组。 如果可以编写任何功能,则只有我拥有该子项键的东西。

我希望这在php数组函数中应该可行。

1 个答案:

答案 0 :(得分:1)

您很亲密-您所需要的只是使用 gradle bootRun --stacktrace FAILURE: Build failed with an exception. * What went wrong: Could not determine java version from '12'. * Try: Run with --info or --debug option to get more log output. * Exception is: java.lang.IllegalArgumentException: Could not determine java version from '12'. at org.gradle.api.JavaVersion.toVersion(JavaVersion.java:63) at org.gradle.api.JavaVersion.current(JavaVersion.java:72) at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:32) at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:24) at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:206) at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:169) at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33) at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22) at org.gradle.launcher.Main.doAction(Main.java:33) at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:567) at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:54) at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:35) at org.gra dle.launcher.GradleMain.main(GradleMain.java:23) -这将发送该数组作为参考,因此对他的任何更改都将保留在原始数组上。可以在PHP manual中查找更多知识。

请考虑以下内容:

&

实时示例:https://3v4l.org/ZbiB9