0

I am using an API which returns data as a string in the following format:

{"domain.com":{"status":"available","classkey":"domcno"}}

I want to put this result in to a multi-dimensional PHP array, but since the input is a string, I can't think of a convenient way to convert this.

Is there a function that can automatically parse this data into an array as desired?

6
  • 2
    Yes: json_decode(). Commented Apr 8, 2014 at 14:52
  • 1
    codepad.viper-7.com/rOsaxH Commented Apr 8, 2014 at 14:52
  • Same solution, but a JSON file is not quite the same as a Hashmap. I couldn't find a duplicate by searching by what I thought I was looking for, so if nothing else the wording of this question will be useful to others. Commented Apr 8, 2014 at 15:07
  • @monkeymatrix: Does the API explicitly state that the format of the data is a (serialized) hashmap rather than JSON? Commented Apr 8, 2014 at 15:13
  • 1
    Interesting. Maybe it's because the data that's returned doesn't necessarily conform to the JSON spec (hence, not be JSON). You'll probably be able to json_decode() it most of the time as others have said, but you should still be careful. Commented Apr 8, 2014 at 15:22

2 Answers 2

1

That's JSON, simple enough:

$array = json_decode($string, true);

Yields:

Array
(
    [domain.com] => Array
        (
            [status] => available
            [classkey] => domcno
        )

)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Seems obvious now.
1
$j = '{"domain.com":{"status":"available","classkey":"domcno"}}';
$d = json_decode($j,true);

print_r($d);

Comments

Your Answer

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