2017-06-04 37 views
1

我有一個函數可以顯示從TMDB到我的網站的演員照片是否有一種方法可以讓我們在沒有數組的情況下進行打印,它僅使用print_r進行打印我想知道如果我可以打印它像回聲或打印。顯示沒有數組的值(print_r)

這是代碼:

public function getCasts($movieID) 
{ 
    if (empty($movieID)) return; 

    function cmpcast($a, $b) 
    { 
     return ($a["order"]>$b["order"]); 
    } 

    $temp = $this->_call("movie/" . $movieID . "/casts"); 
    $casts = $temp['cast']; 

    $temp = array(); 
    if (count($casts) > 0) 
    { 
     usort($casts, "cmpcast"); 
     foreach ($casts as &$actor) { 
      if (!empty($actor['profile_path'])) { 
       for ($i=6; $i<count($temp['id']); $i++) 
        if ($temp['name'][$i] == $actor['name']) $temp['char'][$i] .= "/".str_replace('(voice)', '(hang)', $actor['character']); 

       if (!in_array($actor['name'], (array) $temp['name'])) { 
         $temp['pic'][] = "<div style='margin-top:15px;' align='center'><div style='width:140px;margin-right:8px;display:inline-block;vertical-align:top;'><img style='width:130px;height:130px;border-radius:50%;' src='".$this->getImageURL().$actor['profile_path']."'><br />".$actor['name']."<br />".$actor['character']."</div></div>"; 
        } 
      } 
     } 
    } 
    return $temp; 
} 
+0

是的,但你必須指出你的回聲在數組中。像'echo $ temp ['pic'] [0];'。 –

+0

非常感謝,IDK我錯過了這個。 –

+0

順便說一句,有沒有辦法限制它顯示的項目? –

回答

1

你可以使用某種循環。如果您想限制回聲項目的數量,請使用for循環。

例如:

$casts = getCasts(1); 

for ($i = 0; $i < 5; $i++) { 
    if (isset($casts['pic'][$i])) { 
     echo $casts['pic'][$i]; 
    } 
} 

希望它能幫助。

+0

如果數組沒有數字索引 - 這將無法按預期工作。 –

+0

是@u_mulder。你是對的,但是來自操作代碼。該數組有數字索引。 – JazZ