Here’s something simple for someone to answer for me. I’ve tried searching but I don’t know what I’m looking for really.
I have an array from a JSON string, in PHP, of cast and crew members for a movie.
Here I am pulling out only the people with the job name ‘Actor’
foreach ($movies[0]->cast as $cast) {
if ($cast->job == 'Actor') {
echo '<p><a href="people.php?id=' . $cast->id . '">' . $cast->name . ' - ' . $cast->character . '</a></p>';
}
}
The problem is, I would like to be able to limit how many people with the job name ‘Actor’ are pulled out. Say, the first 3.
So how would I pick only the first 3 of these people from this array?
Use a variable called
$num_actorsto track how many you’ve already counted, andbreakout of the loop once you get to 3.