如何从php中的结果获取json键名?

时间:2018-07-28 11:16:35

标签: php json

当art_ean值包含400004471时,我想要键或reference_id的详细信息(因为两者都是相同的值)。

{
    "TD0000000000993": {
        "reference_id": "TD0000000000993",
        "art_ean": "400004481|,400004491|,400004471|"
    },
    "TD0000000000992": {
        "reference_id": "TD0000000000992",
        "art_ean": "400004482|,400004492|,400004472|"
    }
}

1 个答案:

答案 0 :(得分:0)

您的json无效。您应该删除每个对象上的最后一个,。您可以使用列表来获取更多详细信息https://jsonlint.com

这就是我想要的。

<?php

$data = '{
    "TD0000000000993": {
        "reference_id": "TD0000000000993",
        "art_ean": "400004481|,400004491|,400004471|"
    },
    "TD0000000000992": {
        "reference_id": "TD0000000000992",
        "art_ean": "400004482|,400004492|,400004472|"
    }
}';

$decodedData = \json_decode($data, true);

$result = array_column(
    array_filter($decodedData, function($data) {
        return false !== strpos($data['art_ean'], '400004471');
    }),
    'reference_id'
);