简体   繁体   English

如何使用 PHP 将 JSON 节点转换为数组

[英]How to turn JSON nodes into an Array using PHP

I have to filter portions of a JSON, there are different nodes/sub-nodes and I really need only a portion, for example this is what it may look like:我必须过滤 JSON 的部分,有不同的节点/子节点,我真的只需要一部分,例如它可能看起来像这样:

"node1":{         
    "686":{            
        "value1":"686",
        "value2":"M",
        "value3":0
    }
    "687":{            
        "value1":"687",
        "value2":"L",
        "value3":1
    }
    "688":{            
        "value1":"688",
        "value2":"M",
        "value3":0
    }

For example I need to extract the node1 nodes, specifically ["686","687","688"] what I have tried so far:例如,我需要提取node1节点,特别是到目前为止我尝试过的["686","687","688"]

$node1values = array_keys((array)$myjson->node1)

The response is weird: * items consider that I never use PHP regularly, no idea how to get this sorted without having to write a procedure to iterate every possible node.响应很奇怪: * items考虑到我从不经常使用 PHP,不知道如何在不必编写迭代每个可能节点的过程的情况下进行排序。

Use array_column() to get a particular property from each array element.使用array_column()从每个数组元素中获取特定属性。

$node1values = array_column((array)$myjson->node1, 'value1');

DEMO演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM