2019-11-18 05:58:33 +01:00
---
2014-10-27 04:40:02 +01:00
title: Managing Lists
2014-10-28 04:45:46 +01:00
summary: The SS_List interface allows you to iterate through and manipulate a list of objects.
2019-11-18 05:58:33 +01:00
icon: list
---
2014-10-27 04:40:02 +01:00
# Managing Lists
2017-07-03 03:22:12 +02:00
Whenever using the ORM to fetch records or navigate relationships you will receive an [SS_List ](api:SilverStripe\ORM\SS_List ) instance commonly as
either [DataList ](api:SilverStripe\ORM\DataList ) or [RelationList ](api:SilverStripe\ORM\RelationList ). This object gives you the ability to iterate over each of the results or
2014-10-27 04:40:02 +01:00
modify.
## Iterating over the list.
2017-07-03 03:22:12 +02:00
[SS_List ](api:SilverStripe\ORM\SS_List ) implements `IteratorAggregate` , allowing you to loop over the instance.
2014-10-27 04:40:02 +01:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\Security\Member;
2014-10-27 04:40:02 +01:00
2017-10-27 04:38:27 +02:00
$members = Member::get();
foreach($members as $member) {
echo $member->Name;
}
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
Or in the template engine:
2017-08-03 02:51:32 +02:00
```ss
2017-10-27 04:38:27 +02:00
< % loop $Members %>
<!-- -->
< % end_loop %>
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
## Finding an item by value.
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
// $list->find($key, $value);
2014-10-27 04:40:02 +01:00
2017-10-27 04:38:27 +02:00
//
$members = Member::get();
2014-10-27 04:40:02 +01:00
2017-10-27 04:38:27 +02:00
echo $members->find('ID', 4)->FirstName;
// returns 'Sam'
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
## Maps
A map is an array where the array indexes contain data as well as the values. You can build a map from any list
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
$members = Member::get()->map('ID', 'FirstName');
2017-08-03 05:35:09 +02:00
2020-04-20 09:07:53 +02:00
// $members = [
2017-10-27 04:38:27 +02:00
// 1 => 'Sam'
// 2 => 'Sig'
// 3 => 'Will'
2020-04-20 09:07:53 +02:00
// ];
2017-08-03 02:51:32 +02:00
```
2017-07-03 03:22:12 +02:00
This functionality is provided by the [Map ](api:SilverStripe\ORM\Map ) class, which can be used to build a map around any `SS_List` .
2014-10-27 04:40:02 +01:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
$members = Member::get();
$map = new Map($members, 'ID', 'FirstName');
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
## Column
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
$members = Member::get();
2014-10-27 04:40:02 +01:00
2017-10-27 04:38:27 +02:00
echo $members->column('Email');
2017-08-03 05:35:09 +02:00
2020-04-20 09:07:53 +02:00
// returns [
2017-10-27 04:38:27 +02:00
// 'sam@silverstripe.com',
// 'sig@silverstripe.com',
// 'will@silverstripe.com'
2020-04-20 09:07:53 +02:00
// ];
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
## ArrayList
2017-07-03 03:22:12 +02:00
[ArrayList ](api:SilverStripe\ORM\ArrayList ) exists to wrap a standard PHP array in the same API as a database backed list.
2014-10-27 04:40:02 +01:00
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
$sam = Member::get()->byId(5);
$sig = Member::get()->byId(6);
2014-10-27 04:40:02 +01:00
2017-10-27 04:38:27 +02:00
$list = new ArrayList();
$list->push($sam);
$list->push($sig);
2017-08-03 05:35:09 +02:00
2017-10-27 04:38:27 +02:00
echo $list->Count();
// returns '2'
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
2017-11-27 04:39:17 +01:00
## Related Lessons
* [Lists and pagination ](https://www.silverstripe.org/learn/lessons/v4/lists-and-pagination-1 )
2014-10-27 04:40:02 +01:00
## API Documentation
2017-07-03 03:22:12 +02:00
* [SS_List ](api:SilverStripe\ORM\SS_List )
* [RelationList ](api:SilverStripe\ORM\RelationList )
* [DataList ](api:SilverStripe\ORM\DataList )
* [ArrayList ](api:SilverStripe\ORM\ArrayList )
* [Map ](api:SilverStripe\ORM\Map )