duankan8739 2015-03-13 19:33
浏览 36

PHP中的json_decode,具有多个相同的名称但值不同

I am trying to decode a json data set with many entires that have the same names but are paired with multiple different values.

Example:

{"id":"XXX",
"price":"XXX",
"size":"XXX",
"id":"YYY",
"price":"YYY",
"size":"YYY"}

I have no problem building an array when there is only one value for each name by doing this:

$array = json_decode($array4,true);

PHP array:

  array(3) { 
["id"]=> string(3) "XXX" 
["price"]=> string(3) "XXX" 
["size"]=> string(3) "XXX" 
}

but when I try to use the whole file which contents ~4000 different values with paired between ~4 names I get a response of NULL when I want the json_decode to be something like:

   array(3) { 
["id"]=> string(3) ("XXX","YYY") 
["price"]=> string(3) ("XXX","YYY") 
["size"]=> string(3) ("XXX","YYY") 
}

Can anyone help?

  • 写回答

1条回答 默认 最新

  • dta25920 2015-03-13 19:48
    关注

    Update: I needed to built the json input correctly. Like below:

    {"data":[
    {"id":"XXX","price":"XXX","size":"XXX"},
    {"id":"YYY","price":"YYY","size":"YYY"}]
    }
    

    Then json_decode works great.

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号