How to select only the first few elements in an array?

Here is something just for someone to answer for me. I tried searching, but I do not know what I am looking for.

I have an array from a JSON string, in PHP, cast members and crew for the movie.

Here I pull out only people with the name of the work "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 that I would like to limit the number of people named "Actor." Say the first 3.

So, how would I select only the first 3 of these people from this array?

+7
source share
5 answers

Use a variable called $num_actors to keep track of how much you have already counted, and break to exit the loop after you reach 3.

 $num_actors = 0; foreach ( $movies[0]->cast as $cast ) { if ( $cast->job == 'Actor' ) { echo '...'; $num_actors += 1; if ( $num_actors == 3 ) break; } } 
+3
source

OK is a bit overworking for this problem, but perhaps it serves some educational purposes. PHP comes with a set of iterators that can be used to abstract iterate over a given set of elements.

 class ActorIterator extends FilterIterator { public function accept() { return $this->current()->job == 'Actor'; } } $maxCount = 3; $actors = new LimitIterator( new ActorIterator( new ArrayIterator($movies[0]->cast) ), 0, $maxCount ); foreach ($actors as $actor) { echo /*... */; } 

Extending the abstract FilterIterator class, we can define a filter that returns only participants from this list. LimitIterator allows LimitIterator to limit the iteration to a given set and ArrayIterator is a simple helper to make native arrays compatible with the Iterator interface. Iterators allow the developer to create chains that define the iteration process, which makes them extremely flexible and powerful.

As I said in the introduction: this problem can be easily solved without this Iterator material, but it provides the developer with some advanced options and allows you to reuse the code. You could, for example, upgrade ActorIterator to some CastIterator , which allows you to pass the type of fill to filter in the constructor.

+4
source
 $actors=array_filter($movies[0]->cast, function ($v) { return $v->job == 'Actor'; }); $first3=array_slice($actors, 0, 3); 

or even

 $limit=3; $actors=array_filter($movies[0]->cast, function ($v) use (&$limit) { if ($limit>0 && $v->job == 'Actor') { $limit--; return true; } return false; }); 
+3
source

Add a counter and an if statement.

 $count = 0; 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>'; if($count++ >= 3) break; } } 
+1
source
 $limit = 3; $count = 0; foreach ($movies[0]->cast as $cast) { // You can move the code up here if all you're getting is Actors if ($cast->job == 'Actor') { if ($count == $limit) break;// stop the loop if ($count == $limit) continue;// OR move to next item in loop $count++; echo '<p><a href="people.php?id=' . $cast->id . '">' . $cast->name . ' - ' . $cast->character . '</a></p>'; } } 
+1
source

All Articles