2014-10-26 93 views
0

花了幾個小時閱讀,研究,並不能弄明白,這裏是我的代碼:不能在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應該刪除空值?我只想用一無所有來替換錯誤。

+0

我可以建議你添加'$ post'對象的轉儲嗎?我們不能_guess_什麼URL回覆... – arkascha 2014-10-26 16:34:24

+0

另外我有一個印象,你在某種程度上混合對象和數組類型在這裏... – arkascha 2014-10-26 16:35:40

+0

你是不固定的職位,然後立即重新分配值,這意味着您的$ post變量將存在,也array_filter應該去你的foreach()循環外 – Loopo 2014-10-26 16:47:39

回答

0

我建議改變你的循環,如下所示:

只有當找不到'埃及'時纔打印出來。

<?php foreach ($result->data as $post){ 
    if (stripos($post->caption->text,'egypt') === false) { 
     $post = (str_split($post->caption->text));?> 
     <img src="<?= $post->images->low_resolution->url?>" /> 
     <?= $post->caption->text ?> 
<?php 
    } 
    else{ 
     unset $post; 
    } 
?> 

或者,您可以先清理一個循環中的數組,然後在另一個循環中打印您的條目。這可能會使以後添加額外的「過濾器」變得更加容易。

<?php 
    //edit --- typos clean_aray = array(); 
    $clean_array = array(); 
    foreach ($result->data as $post){ 
     if (stripos($post->caption->text,'egypt') === false) { 
     $clean_array[] = $post; 
     } 
    } 
    foreach ($clean_array as $post){ 
     $post = (str_split($post->caption->text));?> 
     <img src="<?= $post->images->low_resolution->url?>" /> 
     <?= $post->caption->text ?> 
<?php 
    } 
?> 
+0

使用你的建議我沒有太多進展,我真的很生疏,我使用你的建議和代碼片段得到了所有值的空值。 – Tony 2014-10-27 01:15:40

+0

我無法弄清楚你正在用str_split()調用什麼。在那個位置,它會用包含$ post-> caption-> text的數組覆蓋$ post。我認爲你的代碼的這部分工作正如你在你的問題中指出的那樣。 $ result->數據來自哪裏?您可能需要以不同方式訪問變量。 – Loopo 2014-10-28 09:47:31

0

您可以使用array_filter與像strlen的一個回調函數,這將刪除NULL,空字符串,假和假我的意思是明確爲FALSE,而不是0

$result = array_filter($array, 'strlen');