[英]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.