解析具有多个数组的JSON字符串

时间:2021-11-12 02:06:08

I am trying to parse this JSON string

我试图解析这个JSON字符串

$json = {"fields":{
"relationshipStatus":[{"fieldId":4,"name":"Committed"},{"fieldId":2,"name":"Dating"},{"fieldId":6,"name":"Engaged"},{"fieldId":3,"name":"Exclusive"},{"fieldId":7,"name":"Married"},{"fieldId":8,"name":"Open Relationship"},{"fieldId":5,"name":"Partnered"},{"fieldId":1,"name":"Single"}],
            "ethnicity":[{"fieldId":1,"name":"Asian"},{"fieldId":2,"name":"Black"},{"fieldId":3,"name":"Latino"},{"fieldId":4,"name":"Middle Eastern"},{"fieldId":5,"name":"Mixed"},{"fieldId":6,"name":"Native American"},{"fieldId":8,"name":"Other"},{"fieldId":9,"name":"South Asian"},{"fieldId":7,"name":"White"}],
}}

Using this foreach loop, ultimately I want to be able to take the data and use them as Select / List dropdowns on a form.

使用这个foreach循环,最终我希望能够获取数据并将其用作表单上的Select / List下拉列表。

foreach($json['fields'] as $item){
    foreach($item['relationshipStatus'] as $relationship){
        echo $relationship['name'] . " " . $relationship['fieldId'] . "<br/>";
    }

    foreach($item['ethnicity'] as $ethnicity){
        echo $ethnicity['name'] . " " . $ethnicity['fieldId'] . "<br/>";
    }
}

No matter how I try to pull the data out, I keep getting errors similar to:

无论我如何尝试提取数据,我都会遇到类似以下错误:

Notice: Undefined index: relationshipStatus in /Applications/MAMP/htdocs/updateprofile.php on line 126 Warning: Invalid argument supplied for foreach() in /Applications/MAMP/htdocs/updateprofile.php on line 126

注意:未定义的索引:第126行的/Applications/MAMP/htdocs/updateprofile.php中的relationshipStatus警告:在第126行的/Applications/MAMP/htdocs/updateprofile.php中为foreach()提供的参数无效

What am I doing wrong?

我究竟做错了什么?

2 个解决方案

#1


1  

The first foreach selects already relationshipStatus and ethnicity. Maybe the following changes show what I mean:

第一个foreach选择已经关系状态和种族。也许以下更改显示我的意思:

foreach($json['fields'] as $key=>$item){
if ($key == 'relationshipStatus')
foreach($item as $relationship){
    echo $relationship['name'] . " " . $relationship['fieldId'] . "<br/>";
}
else if ($key == 'ethnicity')
foreach($item as $ethnicity){
    echo $ethnicity['name'] . " " . $ethnicity['fieldId'] . "<br/>";
}
}

#2


0  

Here, you iterating the JSON Object in a wrong way.The array values you want to fetch is actually under $json['fields']['relationshipStatus'].

在这里,您以错误的方式迭代JSON对象。您要获取的数组值实际上在$ json ['fields'] ['relationshipStatus']下。

var name = $json['fields']['relationshipStatus']['name'];
var fieldId = $json['fields']['relationshipStatus']['fieldId'];

=============================== OR ========================================

=============================== OR ================== ======================

As described by the A.fink

正如A.fink所描述的那样

foreach($json['fields'] as $key=>$item){
if ($key == 'relationshipStatus')
foreach($item as $relationship){
    echo $relationship['name'] . " " . $relationship['fieldId'] . "<br/>";
}
else if ($key == 'ethnicity')
foreach($item as $ethnicity){
    echo $ethnicity['name'] . " " . $ethnicity['fieldId'] . "<br/>";
}
}

#1


1  

The first foreach selects already relationshipStatus and ethnicity. Maybe the following changes show what I mean:

第一个foreach选择已经关系状态和种族。也许以下更改显示我的意思:

foreach($json['fields'] as $key=>$item){
if ($key == 'relationshipStatus')
foreach($item as $relationship){
    echo $relationship['name'] . " " . $relationship['fieldId'] . "<br/>";
}
else if ($key == 'ethnicity')
foreach($item as $ethnicity){
    echo $ethnicity['name'] . " " . $ethnicity['fieldId'] . "<br/>";
}
}

#2


0  

Here, you iterating the JSON Object in a wrong way.The array values you want to fetch is actually under $json['fields']['relationshipStatus'].

在这里,您以错误的方式迭代JSON对象。您要获取的数组值实际上在$ json ['fields'] ['relationshipStatus']下。

var name = $json['fields']['relationshipStatus']['name'];
var fieldId = $json['fields']['relationshipStatus']['fieldId'];

=============================== OR ========================================

=============================== OR ================== ======================

As described by the A.fink

正如A.fink所描述的那样

foreach($json['fields'] as $key=>$item){
if ($key == 'relationshipStatus')
foreach($item as $relationship){
    echo $relationship['name'] . " " . $relationship['fieldId'] . "<br/>";
}
else if ($key == 'ethnicity')
foreach($item as $ethnicity){
    echo $ethnicity['name'] . " " . $ethnicity['fieldId'] . "<br/>";
}
}