TryHarder
TryHarder

Reputation: 2777

How to write this json data as an associative array in php?

DETAILS

I am getting the following json data back via an ajax response.

"result":[{"word":"jquery","wordID":"1"},{"word":"github","wordID":"2"}]

I am trying to understand the information I've received (arrays confuse me). To do that I am trying to recreate the structure of the array in php.

If I were to recreate the array above would I write it like this?

$result=Array (
    [0] => Array (
        word => jquery
        wordID => 1        
    ) 
    [1] => Array (
        word => github
        wordID => 2 
    ) 

If no, how would I write it? Thanks.

Upvotes: 0

Views: 153

Answers (2)

Marc B
Marc B

Reputation: 360572

JSON is essentially the right-hand side of an assignment operation in Javascript, e.g.

var foo = [1,2,3];
          ^^^^^^^--- basically json

Your snippet above is, however, NOT valid json. You've got an object, which cannot simply show up as

"key":"value"

it has to be

{"key":"value"}

Upvotes: 1

Satya
Satya

Reputation: 8881

use json_decode to convert json in an array , and then do a var_dump to see if the structure is what you need

Upvotes: 1

Related Questions