1

I am trying to parse this JSON string

$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($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

What am I doing wrong?

2
  • If you pretty-print the json (ie using jsonlint.com) you'll see that the array is actually under $json['fields']['relationshipStatus'], and not directly under $json['fields'] as in your code. Commented Apr 3, 2016 at 21:53
  • You're absolutely correct. Commented Apr 3, 2016 at 21:55

2 Answers 2

1

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

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/>";
}
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

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

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

As described by the 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/>";
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.