0
{
    "listing": {
        "@attributes": {
            "domain": "example.com"
        },
        "tld": "com",
        "sld": "example",
        "owner": "John Smith"
    }
}

I am needing to iterate through this JSON array and put the values into PHP variables so that I can return the values.

Example:

echo $sld;

would print:

example

Would I need to do this with a foreach loop (and if so how would I format this) or is there a easy built in function such as extract() that will do this?

2
  • 1
    you can just use echo $obj->listing->sld; Commented Jul 17, 2013 at 21:02
  • 1
    Have you looked into decoding the JSON using json_decode? If you pass the optional true boolean second parameter, it will convert JSON into an associative array which can easily be manipulated with PHP. Commented Jul 17, 2013 at 21:03

2 Answers 2

2
<?php 
$json = '{
    "listing": {
        "@attributes": {
            "domain": "example.com"
        },
        "tld": "com",
        "sld": "example",
        "owner": "John Smith"
    }
}';

$decoded_array = json_decode($json, true);
echo $decoded_array['listing']['sld'];//example
?> 
Sign up to request clarification or add additional context in comments.

Comments

1

You can use

json_decode('your json string', true);

Which will return an associative array of your string which you can then loop though.

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.