繁体   English   中英

PHP过滤多维数组

[英]Filter multidimensional array in PHP

我试图从下面的多维数组中获取两个值。 我解码了 JSON,然后尝试过滤它,但是,我没有得到任何值。 你能帮我修复这个错误吗?

我尝试获取电子邮件值和产品名称。 我想的是用内爆得到它

$string=implode(', ', array_column($obj, 'email'));

这是我的数组

{
  "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
  }
}
<?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'];
?>

上面的代码会给你答案。

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

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

演示

输出:

hans.muster@email.ch
IGLO 5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM