JSON到PHP关联数组 [英] JSON to PHP Associative array

查看:156
本文介绍了JSON到PHP关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你们中的任何一个人都知道一个很好的方式把它放在一个关联数组中。我试过 json_decode ,但发现它不是很有帮助。



这是我需要放入关联数组的数据:

  {
data:[
{
name:Joe Bloggs,
id 203403465
},
{
name:Fred Bloggs,
id:254706567
},
{
name:Barny Rubble,
id:453363843
},
{
name:Homer Simpson,
id:263508546
}
]
}



< hr>

编辑:



在我接受答案后,我记得为什么我认为json_decode不起作用。



而不是使用关联这样的数组:

  [0] => Array 

[name] => Joe Bloggs
[id] => 203403465

我想要这样一个:

  Array 

[Joe Bloggs] => 45203340465
[Fred Bloggs] => 65034033446

不幸的是,我当时忘了这个..但是我现在解决了我的问题。



感谢您的帮助!

解决方案

json来自ajax ....(否则代码与json_decode一起使用)
所以要确保js json stringifys你的对象和



你需要在json_decode之前的striplashes ;-)在php中


would any of you know a good way to put this into an associative array . I have tried json_decode but found it to not be much help.

This is the data i need to put into an associative array:

   {
       "data": [
          {
             "name": "Joe Bloggs",
             "id": "203403465"
          },
          {
             "name": "Fred Bloggs",
             "id": "254706567"
          },
          {
             "name": "Barny Rubble",
             "id": "453363843"
          },
          {
             "name": "Homer Simpson",
             "id": "263508546"
          }
       ]
    }


EDIT:

After I accepted the answer, I remembered why I thought that the json_decode wasn't working.

Instead of having an associative array like this:

[0] => Array
(
    [name] => Joe Bloggs
    [id] => 203403465
)

I wanted one like this:

Array
(
    [Joe Bloggs] => 45203340465
    [Fred Bloggs] => 65034033446
)

Unfortunately, I had forgotten this at the time.. but I have resolved my issue now anyway.

Thanks for all of your help!

解决方案

i asume your json comes via ajax.... (otherwise the code works with json_decode) so be sure the js json stringifys your object and

you'll need to stripslashes before json_decode ;-) in php

这篇关于JSON到PHP关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆