1

I have a function that displays the actors photos from TMDB to my website is there a way that I can make it print without an array, it prints only with print_r I want to know if I can print it like echo or print.

This is the code:

    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;
}
4
  • Yes, but you'll have to point your echo inside the array. Like echo $temp['pic'][0];. Commented Jun 4, 2017 at 7:42
  • Thank you very much, IDK how I missed that. Commented Jun 4, 2017 at 7:47
  • BTW is there a way to limit the items it displays? Commented Jun 4, 2017 at 7:48
  • You could use a for-loop for that. Like for ($i = 0; $i < 10; $i++) { echo $temp['pic'][$i]; }, this will display 10 images. Commented Jun 4, 2017 at 8:12

1 Answer 1

1

You could use some kind of loop. Use a for loop if you want to limit the number of item you echo.

For example :

$casts = getCasts(1);

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

Hope it helps.

Sign up to request clarification or add additional context in comments.

2 Comments

If array don't have numeric indexes - this will not work as expected.
Yes @u_mulder. You right but from the op code. The array has numeric indexes.