Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php “如何隐藏错误”;试图获取非对象的属性“;_Php_Loops_For Loop_Foreach_Notice - Fatal编程技术网

Php “如何隐藏错误”;试图获取非对象的属性“;

Php “如何隐藏错误”;试图获取非对象的属性“;,php,loops,for-loop,foreach,notice,Php,Loops,For Loop,Foreach,Notice,我有以下代码,可以正常工作。Twitter好友列表正确,但是当显示最后一项时,似乎显示了4次错误“注意:尝试获取非对象的属性” 由于代码正常工作,因此我希望找到一种隐藏这些错误的方法 $connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret); $tweets6 = $connection->get("https://api.twitt

我有以下代码,可以正常工作。Twitter好友列表正确,但是当显示最后一项时,似乎显示了4次错误“注意:尝试获取非对象的属性”

由于代码正常工作,因此我希望找到一种隐藏这些错误的方法

$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets6 = $connection->get("https://api.twitter.com/1.1/friends/list.json?screen_name=".$twitteruser."&count=".$notweets);
foreach ($tweets6 as $tweet)
{
    for($i = 0; $i < count($tweet); $i++)
    {
        echo $tweet[$i] -> name;
        echo "<br />";
    }
}
$connection=getConnectionWithAccessToken($consumerkey、$ConsumerCret、$accesstoken、$accesstokensecret);
$tweets6=$connection->get(“https://api.twitter.com/1.1/friends/list.json?screen_name=“$twitteruser.”&count=“.$notweets”);
foreach($tweets6作为$tweet)
{
对于($i=0;$iname;
回声“
”; } }
替换此:

for($i = 0; $i < count($tweet); $i++)
for($i=0;$i
为此:

for($i = 0; $i < count($tweet) - 1; $i++)
for($i=0;$i
编辑

for($i=0;$iname)){
echo$tweet[$i]->name;
回声“
”; } }

尝试此操作

如果对象在使用其值之前具有特定属性,则可以添加检查器

if (isset($tweet[$i]->name)) {
    // process

}

打印前使用简单的if条件

   $connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
   $tweets6 = $connection->get("https://api.twitter.com/1.1/friends/list.json?screen_name=".$twitteruser."&count=".$notweets);
foreach ($tweets6 as $tweet)
{
   for($i = 0; $i < count($tweet); $i++){  
     if($tweet[$i]){
        echo $tweet[$i] -> name;
        echo "<br />";
     }
   }
}
$connection=getConnectionWithAccessToken($consumerkey、$ConsumerCret、$accesstoken、$accesstokensecret);
$tweets6=$connection->get(“https://api.twitter.com/1.1/friends/list.json?screen_name=“$twitteruser.”&count=“.$notweets”);
foreach($tweets6作为$tweet)
{
对于($i=0;$iname;
回声“
”; } } }
如果为空,则使用该选项以防止该通知

$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets6 = $connection->get("https://api.twitter.com/1.1/friends/list.json?screen_name=".$twitteruser."&count=".$notweets);
foreach ($tweets6 as $tweet)
{
    for($i = 0; $i < count($tweet); $i++)
    {
        if(empty($tweet[$i]->name)) continue;
        echo $tweet[$i]->name;
        echo "<br />";
    }
}
$connection=getConnectionWithAccessToken($consumerkey、$ConsumerCret、$accesstoken、$accesstokensecret);
$tweets6=$connection->get(“https://api.twitter.com/1.1/friends/list.json?screen_name=“$twitteruser.”&count=“.$notweets”);
foreach($tweets6作为$tweet)
{
对于($i=0;$iname))继续;
echo$tweet[$i]->name;
回声“
”; } }
虽然接受的答案会起作用,但PHP有做这项工作的功能,更合适。即使属性有空值,它也将返回true,而
isset()
没有空值

if (property_exists($tweets[$i], "name")) {
    ....
}

不,正确访问这些值,您就不会有问题,不要试图隐藏错误并试图解决它们,
$tweet6
包含什么?$tweet6是一个对象列表,它在我的代码中定义我已经尝试过了,它删除了错误,但也删除了最后一个item@user2675041我已经编辑了我的代码,所以请检查一下,希望能有帮助
if (property_exists($tweets[$i], "name")) {
    ....
}