dow5001
2018-07-28 11:16
浏览 245

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

I want to the key or reference_id detail (as both are same value) when the art_ean value contains 400004471.

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

图片转代码服务由CSDN问答提供 功能建议

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

  {
“TD0000000000993”:{
“reference_id”:“TD0000000000993”,
“art_ean”:“400004481 |,400004491 |,400004471 |”
},  
“TD0000000000992”:{
“reference_id”:“TD0000000000992”,
“art_ean”:“400004482 |,400004492 |,400004472 |”
} 
} 
   \  n 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhuo1498 2018-07-28 11:30
    已采纳

    Your json is not valid. You should remove the last , on each object. You can use a list to have some more details https://jsonlint.com.

    Here is what I believe that you want.

    <?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'
    );
    
    打赏 评论

相关推荐 更多相似问题