2017-05-16 74 views
0

當我運行我的腳本,我收到以下錯誤信息:未定義的屬性:stdclass但屬性確實存在?

PHP Notice: Undefined property: stdClass::$sub in /path/to/cron_monitor.php on line 14 

14號線是這個(包括13號線,因爲它的相關以及):

$data=(object)$obj; 
$subject=$data->sub; 

谷歌/其他堆棧問題告訴我,物業不應該存在的,但如果我不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]); 
?> 
+0

那麼,什麼是'$ obj'你覺得有必要嘗試將其轉換爲對象 – RiggsFolly

+0

那'的var_dump()'之前還顯得有些Ø dd,因爲在屬性上沒有提及像「public/protected/private」這樣的屬性可見性? – RiggsFolly

+0

@RiggsFolly - 我已將我的代碼塊添加到帖子中。也許我正在做一些完全錯誤的事情。這是我第一次認真地試圖用PHP做任何事情,所以在其他地方可能導致這種情況的東西可能完全錯誤。 – Ev1l0rd

回答

0

你不會很足夠深到你的數據結構

它看起來像sub並不總是在所有的occurances存在,這可能是爲什麼您收到錯誤

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; 
     } 
    } 

} 
+0

工作過,但我不得不將'$ obj'和'$ thread'定義爲對象,否則我得到錯誤。感謝一羣幫助我。 – Ev1l0rd

+0

這很奇怪。我用你的JSONString,我不需要 – RiggsFolly

相關問題