duanlvxi8652 2014-02-18 02:03
浏览 79
已采纳

将数组推入JSON但得到了不需要的结果

I suck at formatting, maybe I had a bad foundation. I have a json like this

'first_name'=>'steve', 'msg'=>'something here','profile_id'=>1

and I want to push a new item into it, I wrote

$i = array('first_name'=>'steve', 'msg'=>'something here','profile_id'=>1);
$loginId = array($_GET['login_id']);
array_push($i,$loginId);

echo json_encode($i);

The result I got is strange:

  • 写回答

2条回答 默认 最新

  • dtvx3420 2014-02-18 02:06
    关注
    $i = array('first_name'=>'steve', 'msg'=>'something here','profile_id'=>1);
    $loginId = $_GET['login_id'];
    
    $i['login_id']=$loginId;    
    echo json_encode($i);
    

    The reason array_push didn't work is because you are treating $i as an array (collection) of objects (arrays), while it is just a key-value list (map).

    If the array is like K1=>V1, K2=>V2, use $arr[K3]=V3 to add another pair.

    If the array is like [(k1,v1), (k2,v2)], then array_push($arr,(k3,v3));

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。