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?

  • 2
    Yes: [`json_decode()`](http://php.net/json_decode). – Amal Murali Apr 08 '14 at 14:52
  • 1
    http://codepad.viper-7.com/rOsaxH – John Conde Apr 08 '14 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. –  Apr 08 '14 at 15:07
  • @monkeymatrix: Does the API explicitly state that the format of the data is a (serialized) hashmap rather than JSON? – BoltClock Apr 08 '14 at 15:13
  • @BoltClock Yes it does. –  Apr 08 '14 at 15:15
  • 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. – BoltClock Apr 08 '14 at 15:22

2 Answers2

1

That's JSON, simple enough:

$array = json_decode($string, true);

Yields:

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

)
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
1
$j = '{"domain.com":{"status":"available","classkey":"domcno"}}';
$d = json_decode($j,true);

print_r($d);
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63