Skip to content

Commit

Permalink
added zip
Browse files Browse the repository at this point in the history
  • Loading branch information
izica committed Mar 27, 2019
1 parent 6355ac5 commit 312e81f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
19 changes: 17 additions & 2 deletions PhpCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,21 @@ public function exclude($keys)
return new PhpCollection($arData);
}

public function zip($array){
$arData = [];

$arCollection = array_values($this->arCollection);
$nCount = count($arCollection);

for ($nIndex = 0; $nIndex < $nCount; $nIndex++) {
$arFirstItem = is_array($arCollection[$nIndex]) ? $arCollection[$nIndex] : [$arCollection[$nIndex]];
$arSecondItem = is_array($array[$nIndex]) ? $array[$nIndex] : [$array[$nIndex]];
$arData[] = array_merge($arFirstItem, $arSecondItem);
}

return new PhpCollection($arData);
}

/**
* @param string $glue
* @param string $serializer
Expand Down Expand Up @@ -167,8 +182,8 @@ public function map($function = false)
};
}
$arData = [];
foreach ($this->arCollection as $arItem) {
$arData[] = $function($arItem);
foreach ($this->arCollection as $sKey => $arItem) {
$arData[$sKey] = $function($arItem, $sKey);
}
return new PhpCollection($arData);
}
Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ inspired by Illuminate\Support\Collection and Lodash
composer require izica/php-collection
```

### Notice
Every method create new collection, not mutate data;

```php

$collection = PhpCollection:collect([100, 200, 300, 400]);
$collection2 = $collection->filter(function($item){
return $item > 200;
})

/*
$collection != $collection2
*/

```

### Documentation

* [collect](#collectarray)
Expand Down Expand Up @@ -241,7 +257,7 @@ $collection = PhpCollection:collect($products)->groupBy('category_id')->all();
#### count()
#### all() OR toArray()
#### toJson()

#### zip()

## TODO
#### dumpBrowser()
Expand Down

0 comments on commit 312e81f

Please sign in to comment.