【问题标题】:How to display data in a PHP array so it can be rendered as JSON array and be used in a React Native project如何在 PHP 数组中显示数据,以便可以将其呈现为 JSON 数组并在 React Native 项目中使用
【发布时间】:2022-01-20 23:27:51
【问题描述】:

php数组的内容是这样的:

  $phpArray = 
  [
    "title" => "The Basics - Networking", 
    "description" => "Your app fetched this from a remote endpoint!", 
    "movies" => 
    [
      [
        "id" => $id, 
        "title" => $title, 
        "releaseYear" => $releaseYear
      ]
    ]
  ]; 
}

echo json_encode(array_values($phpArray));

返回:

[
  "The Basics - Networking",
  "Your app fetched this from a remote endpoint!",
  [
    {
      id: "1",
      title: "Star Wars",
      releaseYear: "1977"
    }
  ]
]

但为了让它工作,它应该显示:

{
  title: "The Basics - Networking",
  description: "Your app fetched this from a remote endpoint!",
  movies: [
    {
      id: "1",
      title: "Star Wars",
      releaseYear: "1977"
    }
  ]
}

问题是我如何渲染数据和数组以获得正确的结果(即上面介绍的后一个数组)。

任何有用的提示将不胜感激! :-)

【问题讨论】:

  • 删除array_values,你应该会很好。
  • 谢谢!在看到你的答案之前,我意识到了这一点。 :-) 感谢您的反馈!

标签: php json react-native


【解决方案1】:

嗯,这比我想象的要简单:

echo json_encode(array_values($phpArray));

必须替换为

echo json_encode($phpArray);

【讨论】:

    猜你喜欢
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 2021-07-15
    • 1970-01-01
    • 2011-02-09
    相关资源
    最近更新 更多