为foreach()提供的参数无效(对象数组)

时间:2015-08-25 14:27:29

标签: php

我无法让我的foreach工作,我已经尝试了一切......

我有这个数组($ plugins-> response):

array (size=1)
  'akismet/akismet.php' => 
    object(stdClass)[108]
      public 'id' => string '15' (length=2)
      public 'slug' => string 'akismet' (length=7)
      public 'plugin' => string 'akismet/akismet.php' (length=19)
      public 'new_version' => string '3.1.3' (length=5)
      public 'url' => string 'https://wordpress.org/plugins/akismet/' (length=38)
      public 'package' => string 'https://downloads.wordpress.org/plugin/akismet.3.1.3.zip' (length=56)

这就是我尝试做的事情:

foreach($plugins->response as $plugin)
{
    var_dump($plugin);
    $payload['output'] = $payload['output'] . "\rThe plugin : {$plugin->slug} is not up to date";
    $payload['status'] = 1;
}

你知道它为什么不起作用吗? 我知道这是一件基本的事情,但我保证超过一个小时我会尝试这项工作。我是PHP新手。 感谢

1 个答案:

答案 0 :(得分:0)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>StackOverFlow</title>
</head>
<body>
<?php

class Plugins {

    public $id;
    public $slug;
    public $plugin;

    public function __construct()
    {
        $this->id = '15';
        $this->slug ='akismet';
        $this->plugin = 'akismet/akismet.php';
    }


}

$plugins = new Plugins();
var_dump($plugins);


// We can iterate the $plugins like this:
foreach ($plugins as $key => $val) {
    echo "$key = $val <br/>";
}
   
// If you want to avoid the iteration and refer to a specific
// property of the object (properties are the public 
//$id,$slug,$plugin) of the class: 
   
$payload['output'] = $payload['output'] . "\rThe plugin : {$plugins->slug} is not up to date";
$payload['status'] = 1;
var_dump($payload);


?>
</body>
</html>