简体   繁体   中英

Filter multidimensional array in PHP

I'm trying to get two values from a multidimensional array below. I decode the JSON and then I try to filter it, however, I don't get any values then. Can you help me to fix this bug?

I try to get the email value and product name. What I thought is to get it with implode

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

This is my array

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

The above code will give you the answer.

You can use it like a JSON object, simply do $json_obj = json_decode($string); and then

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

Demo

Outputs:

hans.muster@email.ch
IGLO 5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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