如何用PHP解析JSON响应中的内部数组

时间:2015-04-26 21:21:25

标签: php arrays json

假设我有一个JSON响应,就是这样形成的:

results: [
{
warehouse: West Side,
name: "Shipment 1",
contents: [
   {
     id: 94,
     product: "Apples",
     abbreviation: "apl"
   },
   {
     id: 64,
     product: "Grapes",
     abbreviation: "grp"
   },
  {
     id: 86
     product: "Watermelon",
     abbreviation: "wtrmelon"
   },
  {
     id: 15,
     product: "Peaches",
     abbreviation: "pch"
   },
 ]
},
{
warehouse: West Side 2,
name: "Shipment 2",
contents: [
 {
     id: 94,
     product: "Apples",
     abbreviation: "apl"
   },
 ]
}

迭代内部数组(即内容)的正确方法是什么,以查看货件是否包含某个项目,例如苹果?理想情况下,我还想仅返回包含所需物品的货件。我的想法是你将获得内容数组的大小/长度,然后遍历它,寻找字符串“apples”。我是在正确的道路上,还是有更好的方法来做我想做的事情?

0 个答案:

没有答案