2015-12-08 135 views
0

我遇到以下問題: 我使用foreach遍歷有效對象數組。當試圖訪問結果對象或它們的屬性時,我收到了我將嘗試訪問非對象的通知。PHP嘗試獲取有效對象的非對象屬性

下面是代碼:

$schema = json_decode($_POST['d']); 
foreach ($schema->node as $node) { 
    var_dump($node); 
    if ($node->status == 1) { 
     $data = $node->id; 
    } 
} 

的var_dump輸出以下:預先

object(stdClass)#5 (6) { 
    ["status"]=> 
    int(0) 
    ["id"]=> 
    int(1) 
    ["title"]=> 
    string(6) "Sensor" 
    ["script"]=> 
    string(24) "from eZness import swag;" 
    ["x"]=> 
    int(60) 
    ["y"]=> 
    int(80) 
} 

感謝。

UPDATE:

$schema = json_decode($_POST['d']); 
foreach ($schema->node as $node) { 
    var_dump($node); 
    echo $node->status; //Funnily this works 
    $status = $node->status; //while this doesn't 
    if ($node->status == 1) { //and this doesn't as well 
     $data = $node->id; 
    } 
} 

但在取出時的var_dump甚至回聲不工作了。

更新: 已解決。看看應用程序的客戶端部分,在$ schema-> node數組中推入NULL值時出現問題,這當然是非對象。

+0

@MinaAbadir是正確的,你'你的php'ed JSON對象缺少'data'屬性。否則,你並沒有添加你所指的正確的數據對象...... –

+0

但是'$ node'之後被正確地轉儲了,所以'$ schema'應該沒問題。可能這個代碼子集很好。 –

+0

1)請向我們展示您獲得的完整錯誤消息2)您的代碼是否僅循環1次迭代?或者你有更多的對象,你看錯了3)另外,如果你做'var_dump();'請從源代碼的輸出,並在這裏發佈 – Rizier123

回答

0

您正在嘗試訪問$node->data,這不存在。

+0

它確實存在,它是傳入的JSON的一部分串。 –

+0

它沒有打印在var_dump中。 –

+2

這將產生「未定義的屬性」通知,而不是「非對象」通知。 – lafor

相關問題