在PHP对象的JSON数组 [英] Json array of objects in php

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

问题描述

这是我的code生产

{
"aaa":1,
"b":2,
"c":3,
"d":4,
"e":5,
"fff":{"a":11111,"b":222222,"c":33333,"d":444454,"e":55555555}
}

这就是code

and this is the code

<?php
$c = array('a' => 11111, 'b' => 222222, 'c' => 33333, 'd' => 444454, 'e' => 55555555 );
$arr = array('aaa' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5 , 'fff'=>$c);
echo json_encode($arr);
?>

但我想有一些像这样的结构

but I want to have some structure like this

{
"aaa":1,
"b":2,
"c":3,
"d":4,
"e":5,
"fff":{"a":11111,"b":222222,"c":33333,"d":444454,"e":55555555},
"last":[
      {
        "id": 8817,
        "loc": "NEW YORK CITY"
      },
      {
        "id": 2873,
        "loc": "UNITED STATES"
      },
      {
        "id": 1501,
        "loc": "NEW YORK STATE"
      }
    ]
}

我在JSON和PHP新我需要这个快,所以我没有时间去阅读有关此JSON结构......所以,请,如果有人知道如何添加这最后一个元素,请提供一些PHP code。

I am new in json and php and I need this fast so I do not have time to read about this json structure... So please if someone know how to add this last element please provide some php code.

谢谢,

推荐答案


  • 以JSON-CN codeD字符串,并把它传递给json_de code()

  • 的返回值赋值给一个变量

  • 通过该变量var_export()得到一个PH​​P-CN codeD重新串数据的presentation。

例如

<?php
$json = '{
"aaa":1,
"b":2,
"c":3,
"d":4,
"e":5,
"fff":{"a":11111,"b":222222,"c":33333,"d":444454,"e":55555555},
"last":[
      {
        "id": 8817,
        "loc": "NEW YORK CITY"
      },
      {
        "id": 2873,
        "loc": "UNITED STATES"
      },
      {
        "id": 1501,
        "loc": "NEW YORK STATE"
      }
    ]
}';


$php = json_decode($json, true);
echo var_export($php);

打印

array (
  'aaa' => 1,
  'b' => 2,
  'c' => 3,
  'd' => 4,
  'e' => 5,
  'fff' => 
  array (
    'a' => 11111,
    'b' => 222222,
    'c' => 33333,
    'd' => 444454,
    'e' => 55555555,
  ),
  'last' => 
  array (
    0 => 
    array (
      'id' => 8817,
      'loc' => 'NEW YORK CITY',
    ),
    1 => 
    array (
      'id' => 2873,
      'loc' => 'UNITED STATES',
    ),
    2 => 
    array (
      'id' => 1501,
      'loc' => 'NEW YORK STATE',
    ),
  ),
)

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

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