dongxu198714 2012-07-13 08:20
浏览 95
已采纳

将字段追加到数组的末尾

I have an array, and when i echo it i get the following output;

Array[{"name":"Kat","age":"10"}]

Now, i need to add an additional filed into this; so finally it should appear as;

Array[{"message":"Success","name":"Kat","age":"10"}]

if $arr is my array, how am i going to append "message":"Success" ?

Sorry, i don't have any code to demonstrate my workings, i am stuck here. i would appreciate it if anyone can help me.

  • 写回答

6条回答 默认 最新

  • dora0817 2012-07-13 08:27
    关注

    Your array content looks like JSON to me. But, if your array are actually just PHP arrays, then do:

    $arr = array('name' => 'Kat', 'age' => '10');
    $arr['message'] = 'Success';
    

    If it is a JSON encoded array:

    $arr = json_decode('{"name":"Kat","age":"10"}' , true)); //true decodes to an array and not a standard object
    $arr['message'] = 'Success';
    
    echo $arr;
    
    //If you want it back in JSON
    $json = json_encode($arr);
    echo $json;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示