1

I have objects such as these:

class Log
{
    public matches;

    public function __construct()
    {
        $this->matches = array();
    }
}

class Match
{
    public owner;
    public stuff;
}

Throughout my program, I store data into $owner and $stuff and access it via iterating through the array matches for the Log object. What I am wondering is how to get an array containing a list of all the unique owners.

For example, if I have the following information:

Bill SomeStuff
Bob SomeOtherStuff
Stan MoreOtherStuff
Bill MoreOfBillsStuff
Bill BillhasLotsofStuff

How do I go about getting an array that contains merely Bill, Bob, Stan?

zajonc
  • 1,935
  • 5
  • 20
  • 25
  • By creating a new array, then iterating over the matches array and only putting those entries to the new array that match what you ask for. Alternatively you can do this with a `FilterIterator` as well which would prevent copying the data but it won't return an array but an iterator. - But anyway, what have you tried so far, it looks rather simple what you ask so wondering where your problem is. – hakre Nov 08 '11 at 19:46

1 Answers1

0

This is an example function that you could add to the Log class to return a list of owners:

function getOwners()
{
    $owners = array();
    foreach($this->matches as $match)
    {
        $owners[$match->owner] = 0;
    }

    return array_keys($owners);
}
hakre
  • 193,403
  • 52
  • 435
  • 836
  • This is precisely what I was looking for. I'm still relatively new the PHP, so I wonder sometimes what is and isn't possible. This does exactly what I need it to do. Kudos! –  Nov 08 '11 at 20:03