花了幾個小時閱讀,研究,並不能弄明白,這裏是我的代碼:不能在PHP擺脫空元素的
<?php
$userid = "";
$accessToken = "";
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$result = fetchData("https://api.instagram.com/v1/users/{$userid}/media/recent/?access_token={$accessToken}&count=-1");
$result = json_decode($result);
?>
<?php foreach ($result->data as $post):{
if (stripos($post->caption->text,'egypt') !== false) {
unset($post);
$post = (str_split($post->caption->text));
$post = (array_filter($post));
}
}
?>
<img src="<?= $post->images->low_resolution->url?>" />
<?= $post->caption->text ?>
<?php endforeach ?>
正如你可以看到我試圖消除「埃及的答覆「在他們中提到,我成功地通過使用未設置來實現這一點。不過,即使使用array_filter,我仍然會獲得空元素。 HTML看起來像這樣:<img src="" />
(你可以想象)爲埃及照片。
這是我不理解:
<?php foreach ($result->data as $post):
{
if (stripos($post->caption->text,'egypt') !== false) {
}
else{
unset($post);
$posta = (str_split($post->caption->text));
$posta = (array_filter($post));
$posta = array_filter($posta, 'strlen');
}
}
?>
<img src="<?= $post->images->low_resolution->url?>" /><br>
<?= $post->caption->text ?><br><br>
<?php endforeach ?>
給我我想要的(只有埃及的照片和說明)的結果,但它也讓我對每一個PHP錯誤:Warning: array_filter() expects parameter 1 to be array, null given' and
警告:array_filter()期望參數1是數組,null給定`不是array_filter應該刪除空值?我只想用一無所有來替換錯誤。
我可以建議你添加'$ post'對象的轉儲嗎?我們不能_guess_什麼URL回覆... – arkascha 2014-10-26 16:34:24
另外我有一個印象,你在某種程度上混合對象和數組類型在這裏... – arkascha 2014-10-26 16:35:40
你是不固定的職位,然後立即重新分配值,這意味着您的$ post變量將存在,也array_filter應該去你的foreach()循環外 – Loopo 2014-10-26 16:47:39