Php 未定义的属性:stdclass但属性确实存在?

Php 未定义的属性:stdclass但属性确实存在?,php,variables,object,Php,Variables,Object,运行脚本时,会收到以下错误消息: PHP Notice: Undefined property: stdClass::$sub in /path/to/cron_monitor.php on line 14 第14行是这样的(包括第13行,因为它也是相关的): Google/Other stack questions告诉我该属性不应该存在,但是如果我做了var_dump($data),我会得到这个(从更大的部分摘录,但是$data是从4chancatalog.jsonAPI输出的),这表明它

运行脚本时,会收到以下错误消息:

PHP Notice:  Undefined property: stdClass::$sub in /path/to/cron_monitor.php on line 14
第14行是这样的(包括第13行,因为它也是相关的):

Google/Other stack questions告诉我该属性不应该存在,但是如果我做了
var_dump($data)
,我会得到这个(从更大的部分摘录,但是
$data
是从4chan
catalog.json
API输出的),这表明它确实存在:

object(stdClass)#2 (26) {
  ["no"]=>
  int(176833602)
  ["now"]=>
  string(21) "05/15/17(Mon)02:08:45"
  ["name"]=>
  string(9) "Anonymous"
  ["sub"]=>
  string(28) "/dfg/ Dwarf Fortress General"
  ["com"]=>
  string(2173) "(excluded since it just bloats up the question, the string is correct)"
  ["filename"]=>
  string(12) "Human Farmer"
  ["ext"]=>
  string(4) ".jpg"
  ["w"]=>
  int(1530)
  ["h"]=>
  int(1027)
  ["tn_w"]=>
  int(250)
  ["tn_h"]=>
  int(167)
  ["tim"]=>
  float(1494828525011)
  ["time"]=>
  int(1494828525)
  ["md5"]=>
  string(24) "0tPuwatHh8Kq/xHEEaWR2Q=="
  ["fsize"]=>
  int(1205673)
  ["resto"]=>
  int(0)
  ["bumplimit"]=>
  int(0)
  ["imagelimit"]=>
  int(0)
  ["semantic_url"]=>
  string(26) "dfg-dwarf-fortress-general"
  ["custom_spoiler"]=>
  int(1)
  ["replies"]=>
  int(435)
  ["images"]=>
  int(113)
  ["omitted_posts"]=>
  int(432)
  ["omitted_images"]=>
  int(110)
  ["last_replies"]=>
  array(3) {
    [0]=>
    array(6) {
      ["no"]=>
      int(176969172)
      ["now"]=>
      string(21) "05/16/17(Tue)13:14:50"
      ["name"]=>
      string(9) "Anonymous"
      ["com"]=>
      string(171) "(excluded since it just bloats up the question, the string is correct)"
      ["time"]=>
      int(1494954890)
      ["resto"]=>
      int(176833602)
    }
    [1]=>
    array(6) {
      ["no"]=>
      int(176969476)
      ["now"]=>
      string(21) "05/16/17(Tue)13:18:23"
      ["name"]=>
      string(9) "Anonymous"
      ["com"]=>
      string(124) "(excluded since it just bloats up the question, the string is correct)"
      ["time"]=>
      int(1494955103)
      ["resto"]=>
      int(176833602)
    }
    [2]=>
    array(6) {
      ["no"]=>
      int(176969731)
      ["now"]=>
      string(21) "05/16/17(Tue)13:21:20"
      ["name"]=>
      string(9) "Anonymous"
      ["com"]=>
      string(179) "(excluded since it just bloats up the question, the string is correct)"
      ["time"]=>
      int(1494955280)
      ["resto"]=>
      int(176833602)
    }
  }
  ["last_modified"]=>
  int(1494955280)
}
奇怪的是,我后来也有一句话

$threadno=$data->no;
这将返回有效值(整数)

编辑:我的整个代码块:

<?php
error_reporting(E_ALL);
include "chan_archiver.php";
include "config.php";

class threadMonitor extends chan_archiver{
        function monitorCatalog($boardwatch, $filter, $basedescription) {
                $json=json_decode( file_get_contents('http://a.4cdn.org/'.$boardwatch.'/catalog.json'),true);
                $monitordescription=$basedescription.date(DATE_RFC850);
                foreach( $json as $thread ){
                        $arr=$thread['threads'];
                        foreach( $arr as $obj ){
                                $data=(object)$obj;
                                var_dump($data);
                                $subject=$data->sub;
                                if (strpos($subject, $filter) !== false){
                                                $threadno=$data->no;
                                                $this->addThread($threadno, $boardwatch, $monitordescription);
                                }
                        }
                }
        }
}
$t=new threadMonitor();

/* IMPORTANT: Add arguments like this:
  board filter basedescription
  Example crontab command (that last space is needed, the command automatically adds the added date and time to the thread):
        php /path/to/cron_monitor.php vg "/dfg/" "Dwarf Fortress General - "
*/

$t->monitorCatalog($argv[1],$argv[2],$argv[3]);
?>

您对数据结构的了解不够深入

而且看起来
sub
并不总是存在于所有的事件中,这可能就是为什么会出现错误

foreach( $json as $obj ){

    foreach ( $obj->threads as $thread ) {
        echo $thread->no . PHP_EOL;
        echo $thread->now . PHP_EOL;
        if ( isset($thread->sub) ) {
            echo $thread->sub . PHP_EOL;
        }
    }

}

因此,在您觉得有必要尝试将其强制转换为对象之前,
$obj
是什么呢
var_dump()
看起来也有点奇怪,因为在属性上没有提到像
public/protected/private
这样的属性可见性?@riggsfully-我已将我的代码块附加到帖子上。也许我做错了什么。这是我第一次认真尝试用PHP做任何事情,因此可能是其他地方完全错误导致了这种情况well@RiggsFolly-是一个。工作正常,但我必须将
$obj
$thread
都定义为对象,否则我会出错。非常感谢你帮助我,真奇怪。我使用了你的JSONString,我不需要
foreach( $json as $obj ){

    foreach ( $obj->threads as $thread ) {
        echo $thread->no . PHP_EOL;
        echo $thread->now . PHP_EOL;
        if ( isset($thread->sub) ) {
            echo $thread->sub . PHP_EOL;
        }
    }

}