php中的Json对象数组

发布于 2024-12-14 11:54:38 字数 904 浏览 0 评论 0原文

这是我的代码生成的 json

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

,这是代码

<?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);
?>

,但我想要一些像这样的结构,

{
"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 代码。

谢谢,

this is the json that my code produces

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

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"
      }
    ]
}

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.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南渊 2024-12-21 11:54:38
  • 获取“json 编码”字符串并将其传递给 json_decode()
  • 将返回值分配给变量
  • ,将该变量传递给 var_export() 以获取数据的“php 编码”字符串表示形式。

例如

<?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',
    ),
  ),
)
  • Take the "json-encoded" string and pass it to json_decode()
  • assign the return value to a variable
  • pass that variable to var_export() to get a "php-encoded" string representation of the data.

e.g.

<?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);

prints

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',
    ),
  ),
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文