LMongo\Eloquent\Collection::lists PHP Method

lists() public method

Get an array with the values of a given key.
public lists ( $value, string $key = null ) : array
$key string
return array
    public function lists($value, $key = null)
    {
        $results = array();
        foreach ($this->items as $item) {
            // If the key is "null", we will just append the value to the array and keep
            // looping. Otherwise we will key the array using the value of the key we
            // received from the developer. Then we'll return the final array form.
            if (is_null($key)) {
                $results[] = $item->{$value};
            } else {
                $results[$item->{$key}] = $item->{$value};
            }
        }
        return $results;
    }

Usage Example

 public function testLists()
 {
     $data = new Collection(array((object) array('name' => 'taylor', 'email' => 'foo'), (object) array('name' => 'dayle', 'email' => 'bar')));
     $this->assertEquals(array('taylor' => 'foo', 'dayle' => 'bar'), $data->lists('email', 'name'));
     $this->assertEquals(array('foo', 'bar'), $data->lists('email'));
 }