Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pick only the first few items in an array?

Tags:

arrays

php

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?

like image 685
Rowan Avatar asked May 01 '11 23:05

Rowan


People also ask

How do I find the first object of an array of objects?

To get first element from array, use var first = array[0]; To get Name from it, use first.Name .

How do you split items in an array?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.


2 Answers

OK - this is a bit of over-kill for this problem, but perhaps it serves some educational purposes. PHP comes with a set of iterators that may be used to abstract iteration over a given set of items.

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 /*... */;
}

By extending the abstract class FilterIterator we are able to define a filter that returns only the actors from the given list. LimitIterator allows you to limit the iteration to a given set and the ArrayIterator is a simple helper to make native arrays compatible with the Iterator interface. Iterators allow the developer to build chains that define the iteration process which makes them extremely flexible and powerful.

As I said in the introduction: the given problem can be solved easily without this Iterator stuff, but it provides the developer with some extended options and enables code-reuse. You could, for example, enhance the ActorIterator to some CastIterator that allows you to pass the cast type to filter for in the constructor.

like image 75
Stefan Gehrig Avatar answered Oct 13 '22 23:10

Stefan Gehrig


Use a variable called $num_actors to track how many you've already counted, and break out of the loop once you get to 3.

$num_actors = 0;
foreach ( $movies[0]->cast as $cast ) {
    if ( $cast->job == 'Actor' ) {
        echo '...';

        $num_actors += 1;
        if ( $num_actors == 3 )
            break;
    }
}
like image 27
Jon Gauthier Avatar answered Oct 14 '22 01:10

Jon Gauthier