【问题标题】:Why am I getting a Warning error in this PHP foreach loop?为什么我在这个 PHP foreach 循环中收到警告错误?
【发布时间】:2019-01-08 01:57:33
【问题描述】:

我正在学习使用 foreach 语句循环遍历 json 文件的教程。当然,在示例中,我遵循这个工作正常,但我似乎无法让它在我的版本上工作。我认为该错误表明我实际上并没有传递数组。这是否意味着问题出在 json 文件中?还是有语法问题?

警告:在第 27 行的 C:\xampp\htdocs\loops\json_example.php 中为 foreach() 提供的参数无效

JSON 文件:movies.json

{ //json object
"movies": [ //movies = array
    {
        "title": "The Godfather",
        "year": "1972",
        "genre": "Drama",
        "director": "Francis Ford Copolla"
    },
    {
        "title": "Superbad",
        "year": "2007",
        "genre": "Comedy",
        "director": "Greg Mottola"
    },
    {
        "title": "The Departed",
        "year": "2006",
        "genre": "Drama",
        "director": "Martin Scorsese"
    },
    {
        "title": "Saving Private Ryan",
        "year": "1998",
        "genre": "Action",
        "director": "Steven Spielberg"
    },
    {
        "title": "The Expendables",
        "year": "2010",
        "genre": "Action",
        "director": "Sylvester Stallone"
    }
]
}

PHP 代码:json_example.php

    <?php
    $jsondata = file_get_contents("movies.json"); #set variable, function "file_get_contents" grabs everything in the file. can also use with a website is url is within () to insert entire site.

    $json = json_decode($jsondata, true); #decodes json so that we can parse it
?>

<!DOCTYPE html>
<html>
<head>
    <title>JSON Example</title>
</head>

<body>
    <div id="container">
        <h1>My Favorite Movies</h1>
        <ul>
            <?php      
                foreach($json['movies'] as $key => $value) {
                    echo '<h4>'.$value['title'].'</h4>';
                    echo '<li>Year: '.$value['year'].'</li>';
                    echo '<li>Genre: '.$value['genre'].'</li>';
                    echo '<li>Director: '.$value['director'].'</li>';
                }
            ?>
        </ul>
    </div>

</body>

</html>

请原谅我的评论,我还在学习。

【问题讨论】:

  • 它是一个对象,而不是一个数组。像$json-&gt;movies 一样访问它。另外,学会print_r()var_dump() PHP变量,这样你就不会瞎了。
  • @MonkeyZeus 他使用 true 作为第二个参数,所以这个强制关联数组
  • 希望 JSON 中没有这些 cmets?
  • 当然要去掉json文件中的cmets。
  • 删除 json 中可以正常工作的 cmets。 json_decode 失败

标签: php arrays json foreach


【解决方案1】:

由于 json 格式错误,您的 json_decode 失败。 错误的 JOSN 格式

$test =' { //json object
    "movies": [ //movies = array
        {
            "title": "The Godfather",
            "year": "1972",
            "genre": "Drama",
            "director": "Francis Ford Copolla"
        }]}';

json 中的任何 cmets 都将被视为 json 字符串,您的 json 解码由于格式错误而失败

$decode = json_decode($test,TRUE) ; // fails to decode due to bad json if you var_dump($decode) result in null

去掉json字符串里面的cmets

$test =' { 
    "movies": [ 
        {
            "title": "The Godfather",
            "year": "1972",
            "genre": "Drama",
            "director": "Francis Ford Copolla"
        }]}';

【讨论】:

  • 这是问题,谢谢!在我努力记录所学内容的过程中,我没有意识到使用 JSON 文件,其中的任何 cmets 都被视为字符串。我基本上是通过试图彻底地破坏自己的大声笑。
【解决方案2】:

为 foreach() 提供的参数无效

表示你使用的$json['movies']

foreach($json['movies'] as $key => $value) {

不是数组或对象。 The PHP documentation states:

foreach 仅适用于数组和对象,当您尝试在具有不同数据类型的变量或未初始化的变量上使用它时会发出错误。

理论上,$json = json_decode($jsondata, true); 应该生成一个带有键 'movies' 的数组,其中包含一个电影数组,但由于您收到无效参数警告,这意味着由于某种原因没有发生。

如果您的 movies.json 文件确实包含 cmets,那就是原因。正如其他人所说,JSON cannot contain comments

如果文件中没有真正的 cmets,或者如果您删除了 cmets 仍然没有得到您期望的数组,您可以使用 json_last_errorjson_last_error_msg 来帮助诊断问题。

您应该在代码中包含某种错误处理,以应对输入文件因任何原因而无法解析的情况。在这种情况下,可能是因为您在不知不觉中将 cmets 放入其中,但将来如果您从其他来源获取文件,您可能无法控制其内容。只需检查 if (is_array($json['movies'])) 就足以验证您是否有可以使用 foreach 进行迭代的内容。你想如何处理你不想处理的情况取决于你。

【讨论】:

  • 感谢您进一步阐明如何进一步诊断 json_last_error 和 json_last_error_msg 的问题!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-11
  • 2011-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
相关资源
最近更新 更多