简体   繁体   English

如何将这个json数据写成php中的关联数组?

[英]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. 我通过ajax响应得到以下json数据。

"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. 为此,我试图在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. 谢谢。

使用json_decode转换数组中的json,然后执行var_dump以查看结构是否是您需要的

JSON is essentially the right-hand side of an assignment operation in Javascript, eg JSON本质上是Javascript中赋值操作的右侧,例如

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

Your snippet above is, however, NOT valid json. 你上面的代码段,然而, 不是有效的JSON。 You've got an object, which cannot simply show up as 你有一个对象,它不能简单地显示出来

"key":"value"

it has to be 它一定要是

{"key":"value"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM