Skip to content

Commit 42c60dc

Browse files
author
nix
committed
Arr::kvjoin() and Arr::first() now accept iterables
1 parent 92c93f0 commit 42c60dc

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ $callback = fn($carry, $v, $k) => "$carry|$k";
3838
Arr::reduce($array, $callback, "") // => "|a|b|1|2"
3939
```
4040

41-
* `kvjoin(array $data, string $sep = ', ', string $kv_sep = '='): string`<br>
41+
* `kvjoin(iterable $iterable, string $sep = ', ', string $kv_sep = '='): string`<br>
4242
Like implode()/join() in legacy syntax, but outputs the keys too and takes an additional parameter `$kv_sep`.
4343
```php
4444
$array = ['a' => 'A', 'b' => 'B', 1 => 'one', 2 => 'two'];
4545
Arr::kvjoin($array) // => "a=A, b=B, 1=one, 2=two"
4646
```
4747

48-
* `first(array $array): array`<br>
48+
* `first(iterable $iterable): array`<br>
4949
Returns the first mapping (element) of an array as key and value in an array, or null/null if the array is empty.
5050
```php
5151
$array = ['a' => 'A', 'b' => 'B', 1 => 'one', 2 => 'two'];
@@ -62,7 +62,7 @@ Arr::find($array, 'c', 1) // => ['k' => 1, 'v' => 'one']
6262
Arr::find($array, 'c', 3) // => ['k' => null, 'v' => null]
6363
```
6464

65-
The return value for the last two functions are well suited for array destruction and safety on searching:
65+
The return value for the last two functions is well suited for array destruction and safety on searching:
6666
```php
6767
['k' => $key, 'v' => $value] = Arr::find($array, 2, 'b');
6868
if ($key === null)

src/Arr.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static function reduce(iterable $iterable, callable $callback, mixed $ini
7474
* @param string $kv_sep the separator for key and value
7575
* @since 1.0
7676
*/
77-
public static function kvjoin(array $data, string $sep = ', ', string $kv_sep = '='): string
77+
public static function kvjoin(iterable $data, string $sep = ', ', string $kv_sep = '='): string
7878
{
7979
$first = true;
8080
$str = '';
@@ -88,21 +88,21 @@ public static function kvjoin(array $data, string $sep = ', ', string $kv_sep =
8888

8989
/**
9090
* Returns the first mapping (element) of an array as key and value in an array, or null/null if the array is empty.
91-
* Array destructuring is a very convenient way to handle the result: `['k' => $key, 'v' => $value] = array_first($array)`
92-
* @param array $array the source array
91+
* Array destructuring is a very convenient way to handle the result: `['k' => $key, 'v' => $value] = Arr::first($array)`
92+
* @param iterable $iterable the source iterable
9393
* @return array ['k' => <key>/null, 'v' => <value>/null]
9494
* @since 1.0
9595
*/
96-
public static function first(array $array): array
96+
public static function first(iterable $iterable): array
9797
{
98-
foreach ($array as $k => $v)
98+
foreach ($iterable as $k => $v)
9999
return compact('k', 'v');
100100
return ['k' => null, 'v' => null];
101101
}
102102

103103
/**
104104
* Searches for a mapping in the array whose key is one of $keys, returns the key and the value or null/null (if not found) as an array.
105-
* Array destructuring is a very convenient way to handle the result: `['k' => $key, 'v' => $value] = array_find($array, 'key1', 'key2')`
105+
* Array destructuring is a very convenient way to handle the result: `['k' => $key, 'v' => $value] = Arr::find($array, 'key1', 'key2')`
106106
* @param array $array the source array
107107
* @param string|int ...$ks the keys
108108
* @return array ['k' => <key>/null, 'v' => <value>/null]

0 commit comments

Comments
 (0)