【问题标题】:How to convert a nested JSON array in PHP [duplicate]如何在 PHP 中转换嵌套的 JSON 数组 [重复]
【发布时间】:2020-09-04 20:25:58
【问题描述】:

我需要转换

h 这个 JSON:

["theory",["theory","theory of relativity","theory test","theory of everything","theory definition","theory of evolution","theory of mind","theory of a deadman","theory of love","theory meaning"]]

到一个数组,包括嵌套,在 PHP 中:

array (
  0 => 'theory',
  1 => 
  array (
    0 => 'theory',
    1 => 'theory of relativity',
    2 => 'theory test',
    3 => 'theory of everything',
    4 => 'theory definition',
    5 => 'theory of evolution',
    6 => 'theory of mind',
    7 => 'theory of a deadman',
    8 => 'theory of love',
    9 => 'theory meaning',
  ),
)

在 PHP 中转换为对象后,我需要能够访问/找到对象中的“相对论”。

【问题讨论】:

标签: php json var-dump


【解决方案1】:

你应该使用https://www.php.net/manual/en/function.json-decode.php

例子:

$myJson = '["theory",["theory","theory of relativity","theory test","theory of everything","theory definition","theory of evolution","theory of mind","theory of a deadman","theory of love","theory meaning"]]';

print_r(json_decode($myJson));

问:如何获取“相对论”?:

答:

$data = json_decode($myJson);
echo $data[1][1];

【讨论】:

  • 如何获得“相对论”?
猜你喜欢
  • 2013-12-14
  • 2019-03-27
  • 2018-10-09
  • 1970-01-01
  • 1970-01-01
  • 2018-05-15
  • 2022-08-19
  • 2018-12-27
  • 1970-01-01
相关资源
最近更新 更多