找出php中多维数组中键的值

时间:2018-12-05 05:30:25

标签: php arrays jsondecoder

在我的php查询中,我得到以下输出:

{"projects":[{"id":127,"name":"efrat","status":{"id":10,"name":"development","label":"development"},"description":"","enabled":true,"view_state":{"id":10,"name":"public","label":"public"},"access_level":{"id":90,"name":"administrator","label":"administrator"},"custom_fields":[{"id":1,"name":"Customer email","type":"email","default_value":"","possible_values":"","valid_regexp":"","length_min":0,"length_max":50,"access_level_r":{"id":10,"name":"viewer","label":"viewer"},"access_level_rw":{"id":10,"name":"viewer","label":"viewer"},"display_report":true,"display_update":true,"display_resolved":true,"display_closed":true,"require_report":false,"require_update":false,"require_resolved":false,"require_closed":false}],"versions":[],"categories":[{"id":93,"name":"Monitor","project":{"id":0,"name":null}},{"id":31,"name":"Proactive","project":{"id":0,"name":null}},{"id":30,"name":"Project","project":{"id":0,"name":null}},{"id":29,"name":"Support","project":{"id":0,"name":null}}]}]}

在使用'json_decode'方法后,我得到了: “(

[projects] => Array
    (
        [0] => Array
            (
                [id] => 127
                [name] => myprojectname
                [status] => Array
                    (
                        [id] => 10
                        [name] => development
                        [label] => development
                    )

                [description] =>
                [enabled] => 1
                [view_state] => Array
                    (
                        [id] => 10
                        [name] => public
                        [label] => public
                    )

                [access_level] => Array
                    (
                        [id] => 90
                        [name] => administrator
                        [label] => administrator
                    )

                [custom_fields] => Array
                    (
                        [0] => Array
                            (
                                [id] => 1
                                [name] => Customer email
                                [type] => email
                                [default_value] =>
                                [possible_values] =>
                                [valid_regexp] =>
                                [length_min] => 0
                                [length_max] => 50
                                [access_level_r] => Array
                                    (
                                        [id] => 10
                                        [name] => viewer
                                        [label] => viewer
                                    )

                                [access_level_rw] => Array
                                    (
                                        [id] => 10
                                        [name] => viewer
                                        [label] => viewer
                                    )

                                [display_report] => 1
                                [display_update] => 1
                                [display_resolved] => 1
                                [display_closed] => 1
                                [require_report] =>
                                [require_update] =>
                                [require_resolved] =>
                                [require_closed] =>
                            )

                    )

                [versions] => Array
                    (
                    )

                [categories] => Array
                    (
                        [0] => Array
                            (
                                [id] => 93
                                [name] => Monitor
                                [project] => Array
                                    (
                                        [id] => 0
                                        [name] =>
                                    )

                            )

                        [1] => Array
                            (
                                [id] => 31
                                [name] => Proactive
                                [project] => Array
                                    (
                                        [id] => 0
                                        [name] =>
                                    )

                            )

                        [2] => Array
                            (
                                [id] => 30
                                [name] => Project
                                [project] => Array
                                    (
                                        [id] => 0
                                        [name] =>
                                    )

                            )

                        [3] => Array
                            (
                                [id] => 29
                                [name] => Support
                                [project] => Array
                                    (
                                        [id] => 0
                                        [name] =>
                                    )

                            )

                    )

            )

    )

)”

在我的PHP中,如何从该数组释放“名称”对象值(结果应为“ myprojectname”)?我已经尝试了许多foreach循环,但都无济于事。

谢谢

2 个答案:

答案 0 :(得分:0)

似乎您有一个对象,解码时实际上只有一个数组项。因此,就您而言,“ myprojectname”可能只是“ $ projects [0] [‘name’]”

如果有很多数组项,您可以

  foreach($ projects作为$ project){
  echo $ project ['name'];
}
 

编辑:我将提供的对象自己进行json_decoded,它与OP所提供的json_decoded项不匹配-第一张图片显示了所需的var_dump'name'OP的代码,是OP的一部分下面的代码:

  $ decoded = json_decode($ obj);
$ projects = $ decoded-> projects;
$ name = $ projects [0]-> name;
 

”此处显示的解析输出OP“ 请求的“名称”

答案 1 :(得分:0)

您的“项目”包含一个数组() .call(xAxis) .selectAll("text") .attr("y", 0) .attr("x", 25) .attr("dy", ".35em") .attr("font-family", "ibm-plex-sans") .attr("transform", "rotate(90)");)。我假设“项目”数组可能包含多个“项目”对象,像这样吗?

"projects":[{"id":127, ... }]

在这种情况下,您需要箭头符号来访问name属性,例如:

{
    "projects":
    [
        {
            "id":127,
            "name":"my-project"
        },
        {
            "id":128,
            "name":"my-other-project"
        }
    ]
}

编辑:

我以OP的最小代码示例为例,并得到了预期的结果:

enter image description here

您能否在原始问题的代码段中添加更多详细信息,或者向我们提供您的代码示例? 有一些在线PHP沙箱可以帮助您解决此问题。例如:我去除了所有与您的问题无关的代码,并以两种不同的方式获得了您正在寻找的结果: http://sandbox.onlinephpfunctions.com/code/009c53671fd9545e4fcecfe4b0328974381ee2ce

总结您已经尝试过的所有foreach循环也是一个好主意,因此我们可以查看您是否有自己的解决方案。这样,我们可以更好地理解您的问题,并且使我们无法提供您已经使用的解决方案。