在PHP中过滤多维数组

时间:2019-12-03 08:55:29

标签: php arrays json multidimensional-array

我正在尝试从下面的多维数组中获取两个值。我解码了JSON,然后尝试对其进行过滤,但是,那时我没有任何值。您能帮我解决此错误吗?

我尝试获取电子邮件值和产品名称。我以为是内爆

ReadContext ctx=JsonPath.using(Configuration.builder().options(Option.DEFAULT_PATH_LEAF_TO_NULL).build()).parse(jsonData);
System.out.println(ctx.read("$.store.book..isbn"));

这是我的数组

[null,null,"0-553-21311-3","0-395-19395-8"]

2 个答案:

答案 0 :(得分:1)

<?php
$json = 
'{
  "key": "sendMail",
  "cartItems": [
    {
      "bezeichnung": "test",
      "productname": "IGLO 5",
      "producttype": "Kunststoffsysteme",
      "windowtype": "Quadratisch",
      "typeopen": "rechts_kipp_open_mitte",
      "windowlong": "1000",
      "windowwide": "500",
      "dichtungen": "schwarz",
      "dekorfarbe": "",
      "outColorIn": "Grau",
      "outColorOut": "Grau",
      "selectedGriffe": "Standardgriff",
      "selectedOrnament": "Keine Angaben",
      "selectedVerglasung": "Keine Angaben",
      "selectedKante": "keine",
      "selectedSprossentyp": "Keine Angaben",
      "selectedSprossenmuster": "Keine Angaben",
      "selectedSprossenfarbe": "Keine Angaben",
      "selectedRahmenLinks": "Keine Angaben",
      "selectedRahmenRechts": "Keine Angaben",
      "selectedRahmenOben": "Keine Angaben",
      "selectedRahmenUnten": "Keine Angaben"
    }
  ],
  "personalData": {
    "name": "Hans Muster",
    "email": "hans.muster@email.ch",
    "tel": "0790012345",
    "agb": true
  }
}';
$json_array = json_decode($json,true);
echo $json_array['cartItems'][0]['productname']."<br>";
echo $json_array['personalData']['email'];
?>

上面的代码将为您提供答案。

答案 1 :(得分:1)

您可以像使用JSON对象一样使用它,只需执行$json_obj = json_decode($string);然后

echo $json_obj->personalData->email;
echo PHP_EOL;
echo $json_obj->cartItems[0]->productname;

Demo

输出:

hans.muster@email.ch
IGLO 5