Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/b-viguier/php-emoji


https://github.com/b-viguier/php-emoji

Last synced: 4 days ago
JSON representation

Awesome Lists containing this project

README

        

# Php array functions… with Emojis πŸ™‚

Do you know [Php array functions](https://www.php.net/manual/en/ref.array.php)?
They are powerful, but sometimes tricky to handle… πŸ˜•

Here a cheat-sheet using Emoji, with some of my favorite _tricks_: Enjoy! πŸŽ‰

## Cheat-sheet
### [`array_chunk`](https://www.php.net/manual/en/function.array-chunk.php)
```php
array_chunk([🍎, 🍐, 🍊, πŸ‹, 🍌], 2);
```
```
[
[🍎, 🍐],
[🍊, πŸ‹],
[🍌],
]
```

### [`array_chunk` with preserved keys](https://www.php.net/manual/en/function.array-chunk.php)
```php
array_chunk([🍎, 🍐, 🍊, πŸ‹, 🍌], 2, true);
```
```
[
[🍎, 🍐],
[2 => 🍊, 3 => πŸ‹],
[4 => 🍌],
]
```

### [`array_column`](https://www.php.net/manual/en/function.array-column.php)
```php
array_column(
[
['people' => πŸ˜€, 'fruit' => 🍎, 'animal' => 🐢],
['people' => 😎, 'fruit' => 🍐, 'animal' => 🐭],
['people' => πŸ₯Ά, 'fruit' => 🍊, 'animal' => 🐰],
['people' => 🀑, 'fruit' => πŸ‹, 'animal' => 🦊],
['people' => 🀠, 'fruit' => 🍌, 'animal' => 🐯],
],
'fruit'
);
```
```
[🍎, 🍐, 🍊, πŸ‹, 🍌]
```

### [`array_column` with an index](https://www.php.net/manual/en/function.array-column.php)
```php
array_column(
[
['people' => πŸ˜€, 'fruit' => 🍎, 'animal' => 🐢],
['people' => 😎, 'fruit' => 🍐, 'animal' => 🐭],
['people' => πŸ₯Ά, 'fruit' => 🍊, 'animal' => 🐰],
['people' => 🀑, 'fruit' => πŸ‹, 'animal' => 🦊],
['people' => 🀠, 'fruit' => 🍌, 'animal' => 🐯],
],
'fruit',
'people'
);
```
```
[πŸ˜€ => 🍎, 😎 => 🍐, πŸ₯Ά => 🍊, 🀑 => πŸ‹, 🀠 => 🍌]
```

### [`array_column` all columns with an index](https://www.php.net/manual/en/function.array-column.php)
```php
array_column(
[
['people' => πŸ˜€, 'fruit' => 🍎, 'animal' => 🐢],
['people' => 😎, 'fruit' => 🍐, 'animal' => 🐭],
['people' => πŸ₯Ά, 'fruit' => 🍊, 'animal' => 🐰],
['people' => 🀑, 'fruit' => πŸ‹, 'animal' => 🦊],
['people' => 🀠, 'fruit' => 🍌, 'animal' => 🐯],
],
null,
'people'
);
```
```
[
πŸ˜€ => ['people' => πŸ˜€, 'fruit' => 🍎, 'animal' => 🐢],
😎 => ['people' => 😎, 'fruit' => 🍐, 'animal' => 🐭],
πŸ₯Ά => ['people' => πŸ₯Ά, 'fruit' => 🍊, 'animal' => 🐰],
🀑 => ['people' => 🀑, 'fruit' => πŸ‹, 'animal' => 🦊],
🀠 => ['people' => 🀠, 'fruit' => 🍌, 'animal' => 🐯],
]
```

### [`array_combine`](https://www.php.net/manual/en/function.array-combine.php)
```php
array_combine(
[πŸ˜€, 😎, πŸ₯Ά, 🀑, 🀠],
[🍎, 🍐, 🍊, πŸ‹, 🍌]
);
```
```
[πŸ˜€ => 🍎, 😎 => 🍐, πŸ₯Ά => 🍊, 🀑 => πŸ‹, 🀠 => 🍌]
```

### [`array_count_values`](https://www.php.net/manual/en/function.array-count-values.php)
```php
array_count_values([🍎, 🍎, 🍌, 🍌, 🍎]);
```
```
[🍎 => 3, 🍌 => 2]
```

### [`array_diff`](https://www.php.net/manual/en/function.array-diff.php)
```php
array_diff(
[🍎, 🍐, 🍊, πŸ‹, 🍌],
[❌, 🍐], [❌, 🍊], [❌, πŸ‹]
);
```
```
[0 => 🍎, 4 => 🍌]
```

### [`array_diff_key`](https://www.php.net/manual/en/function.array-diff-key.php)
```php
array_diff_key(
[🐢 => 🍎, 🐭 => 🍐, 🐰 => 🍊, 🦊 => πŸ‹, 🐯 => 🍌],
[🐢 => ❌, 🐭 => ❌], [🐰 => ❌]
);
```
```
[🦊 => πŸ‹, 🐯 => 🍌]
```

### [`array_fill_keys`](https://www.php.net/manual/en/function.array-fill-keys.php)
```php
array_fill_keys([🍎, 🍐, 🍊, πŸ‹, 🍌], βœ…);
```
```
[🍎 => βœ…, 🍐 => βœ…, 🍊 => βœ…, πŸ‹ => βœ…, 🍌 => βœ…]
```

### [`array_fill`](https://www.php.net/manual/en/function.array-fill.php)
```php
array_fill(2, 3, 🍌);
```
```
[2 => 🍌, 3 => 🍌, 4 => 🍌]
```

### [`array_filter`](https://www.php.net/manual/en/function.array-filter.php)
```php
array_filter([🍎, 0, 🍐, false, 🍊, null, πŸ‹, '', 🍌]);
```
```
[0 => 🍎, 2 => 🍐, 4 => 🍊, 6 => πŸ‹, 8 => 🍌]
```

### [`array_filter` with callback](https://www.php.net/manual/en/function.array-filter.php)
```php
array_filter([🍎, 🍐, 🍎, πŸ‹, 🍌], fn($❓) => $❓ === 🍎);
```
```
[0 => 🍎, 2 => 🍎]
```

### [`array_flip`](https://www.php.net/manual/en/function.array-flip.php)
```php
array_flip([🐢 => 🍎, 🐭 => 🍐, 🐰 => 🍎, 🦊 => πŸ‹, 🐯 => 🍌]);
```
```
[🍎 => 🐰, 🍐 => 🐭, πŸ‹ => 🦊, 🍌 => 🐯]
```

### [`array_intersect`](https://www.php.net/manual/en/function.array-intersect.php)
```php
array_intersect(
[🍎, 🍐, 🍊, πŸ‹, 🍌],
[❌, 🍊, 🍐], [🍊, 🍌]
);
```
```
[2 => 🍊]
```

### [`array_intersect_key`](https://www.php.net/manual/en/function.array-intersect-key.php)
```php
array_intersect_key(
[🐢 => 🍎, 🐭 => 🍐, 🐰 => 🍊, 🦊 => πŸ‹, 🐯 => 🍌],
[🐢 => ❌, 🦊 => βœ…], [🦊 => βœ…, 🐯 => ❌]
);
```
```
[🦊 => πŸ‹]
```

### [`array_keys`](https://www.php.net/manual/en/function.array-keys.php)
```php
array_keys(
[🐢 => 🍎, 🐭 => 🍐, 🐰 => 🍊, 🦊 => πŸ‹, 🐯 => 🍌]
);
```
```
[🐢, 🐭, 🐰, 🦊, 🐯]
```

### [`array_map`](https://www.php.net/manual/en/function.array-map.php)
```php
array_map(
fn($πŸ‘€, $🍽) => "$πŸ‘€ ❀️ $🍽",
[πŸ˜€, 😎, πŸ₯Ά, 🀑],
[🍎, 🍐, 🍊, πŸ‹, 🍌]
);
```
```
['πŸ˜€ ❀️ 🍎', '😎 ❀️ 🍐', 'πŸ₯Ά ❀️ 🍊', '🀑 ❀️ πŸ‹', ' ❀️ 🍌']
```

### [`array_map` with `null` callback](https://www.php.net/manual/en/function.array-map.php)
```php
array_map(
null,
[πŸ˜€, 😎, πŸ₯Ά, 🀑, 🀠],
[🍎, 🍐, 🍊, πŸ‹],
[🐢, 🐭, 🐰, 🦊, 🐯]
);
```
```
[
[πŸ˜€, 🍎, 🐢],
[😎, 🍐, 🐭],
[πŸ₯Ά, 🍊, 🐰],
[🀑, πŸ‹, 🦊],
[🀠, null, 🐯],
]
```

### [`array_merge`](https://www.php.net/manual/en/function.array-merge.php)
```php
array_merge(
[🐢 => 🍎, 🐭 => 🍐, 🐰 => ❌],
[🐰 => 🍊, 🦊 => ❌, 🐯 => 🍌],
[🦊 => πŸ‹],
);
```
```
[🐢 => 🍎, 🐭 => 🍐, 🐰 => 🍊, 🦊 => πŸ‹, 🐯 => 🍌]
```

### [Union operator](https://www.php.net/manual/en/language.operators.array.php)
```php
[🐢 => 🍎, 🐭 => 🍐, 🐰 => 🍊] +
[🐰 => ❌, 🦊 => πŸ‹, 🐯 => 🍌] +
[🦊 => ❌];
```
```
[🐢 => 🍎, 🐭 => 🍐, 🐰 => 🍊, 🦊 => πŸ‹, 🐯 => 🍌]
```

### [`array_merge` with integer keys](https://www.php.net/manual/en/function.array-merge.php)
```php
array_merge([🍎, 🍐], [🍊, 🍌], [πŸ‹]);
```
```
[🍎, 🍐, 🍊, 🍌, πŸ‹]
```

### [Union operator with integer keys](https://www.php.net/manual/en/language.operators.array.php)
```php
[🍎, 🍐] + [🍊, 🍌] + [πŸ‹];
```
```
[🍎, 🍐]
```

### [`array_pad`](https://www.php.net/manual/en/function.array-pad.php)
```php
array_pad([🍎, 🍐], 5, 🍌);
```
```
[🍎, 🍐, 🍌, 🍌, 🍌]
```

### [`array_reverse`](https://www.php.net/manual/en/function.array-reverse.php)
```php
array_reverse([🍎, 🍐, 🍊, πŸ‹, 🍌]);
```
```
[🍌, πŸ‹, 🍊, 🍐, 🍎]
```

### [`array_reverse` with preserved keys](https://www.php.net/manual/en/function.array-reverse.php)
```php
array_reverse([🍎, 🍐, 🍊, πŸ‹, 🍌], true);
```
```
[4 => 🍌, 3 => πŸ‹, 2 => 🍊, 1 => 🍐, 0 => 🍎]
```

### [`array_slice`](https://www.php.net/manual/en/function.array-slice.php)
```php
array_slice([🍎, 🍌, 🍌, 🍌, 🍎], 1, 3);
```
```
[🍌, 🍌, 🍌]
```

### [`array_slice` with preserved keys](https://www.php.net/manual/en/function.array-slice.php)
```php
array_slice([🍎, 🍌, 🍌, 🍌, 🍎], 1, 3, true);
```
```
[1 => 🍌, 2 => 🍌, 3 => 🍌]
```

### [`array_unique`](https://www.php.net/manual/en/function.array-unique.php)
```php
array_unique([🍎, 🍎, 🍌, 🍌, 🍎]);
```
```
[0 => 🍎, 2 => 🍌]
```

### [`array_values`](https://www.php.net/manual/en/function.array-values.php)
```php
array_values(
[🐢 => 🍎, 🐭 => 🍐, 🐰 => 🍊, 🦊 => πŸ‹, 🐯 => 🍌]
);
```
```
[🍎, 🍐, 🍊, πŸ‹, 🍌]
```

## Advanced usage

These are what I call _array tipsΒ©_:
* Single statement
* No anonymous function (but short arrow functions are allowed πŸ˜‰)
* 🀯

⚠️ Do not use it in production, unless **ALL** your team understand it! πŸ˜…

### [`array_multisort`: sort multidimensional array](https://www.php.net/manual/en/function.array-multisort.php)
```php
!(
($data = [
['people' => πŸ˜€, 'fruit' => 🍎, 'score' => 2],
['people' => 😎, 'fruit' => 🍐, 'score' => 4],
['people' => πŸ₯Ά, 'fruit' => 🍊, 'score' => 3],
['people' => 🀑, 'fruit' => πŸ‹, 'score' => 1],
['people' => 🀠, 'fruit' => 🍌, 'score' => 5],
]) && array_multisort(
array_column($data, 'score'),
$data
)
) ?: $data;
```
```
[
['people' => 🀑, 'fruit' => πŸ‹, 'score' => 1],
['people' => πŸ˜€, 'fruit' => 🍎, 'score' => 2],
['people' => πŸ₯Ά, 'fruit' => 🍊, 'score' => 3],
['people' => 😎, 'fruit' => 🍐, 'score' => 4],
['people' => 🀠, 'fruit' => 🍌, 'score' => 5],
]
```

### [`array_column`: search last element](https://www.php.net/manual/en/function.array-column.php)
```php
!($data = [
['people' => πŸ˜€, 'fruit' => 🍎, 'animal' => 🐢],
['people' => 😎, 'fruit' => 🍐, 'animal' => 🐭],
['people' => πŸ₯Ά, 'fruit' => 🍊, 'animal' => 🐰],
['people' => 🀑, 'fruit' => πŸ‹, 'animal' => 🦊],
['people' => 🀠, 'fruit' => 🍊, 'animal' => 🐯],
]) ?: array_column($data, null, 'fruit')[🍊] ?? null;
```
```
['people' => 🀠, 'fruit' => 🍊, 'animal' => 🐯]
```

### [`array_intersect`: filtering elements](https://www.php.net/manual/en/function.array-intersect.php)
```php
!($data = [
['people' => πŸ˜€, 'fruit' => 🍎, 'animal' => 🐢],
['people' => 😎, 'fruit' => 🍐, 'animal' => 🐭],
['people' => πŸ₯Ά, 'fruit' => 🍊, 'animal' => 🐰],
['people' => 🀑, 'fruit' => πŸ‹, 'animal' => 🦊],
['people' => 🀠, 'fruit' => 🍊, 'animal' => 🐯],
]) ?: array_values(
array_intersect_key(
$data,
array_intersect(
array_column($data, 'fruit'),
[🍊, 🍎]
)
)
);
```
```
[
['people' => πŸ˜€, 'fruit' => 🍎, 'animal' => 🐢],
['people' => πŸ₯Ά, 'fruit' => 🍊, 'animal' => 🐰],
['people' => 🀠, 'fruit' => 🍊, 'animal' => 🐯],
]
```

### [`array_map`: numerical statistics](https://www.php.net/manual/en/function.array-map.php)
```php
!($data = [1, 2, 3, 4, 5, 6]) ?: [
'count' => $count = count($data),
'average' => $avg = array_sum($data) / $count,
'variance' => array_sum(
array_map(
array_product(...),
array_map(null, $data, $data)
)
) / $count - $avg ** 2,
];
```
```
['count' => 6, 'average' => 3.5, 'variance' => 2.9166666666667]
```

### [`array_map`: matrix transposition](https://www.php.net/manual/en/function.array-map.php)
```php
array_map(
null,
...[
[πŸ˜€, 🍎, 🐢],
[😎, 🍐, 🐭],
[πŸ₯Ά, 🍊, 🐰],
[🀑, πŸ‹, 🦊],
[🀠, 🍊, 🐯],
]
);
```
```
[
[πŸ˜€, 😎, πŸ₯Ά, 🀑, 🀠],
[🍎, 🍐, 🍊, πŸ‹, 🍊],
[🐢, 🐭, 🐰, 🦊, 🐯],
]
```

### [`array_intersect_key`: recursive filtering by keys](https://www.php.net/manual/en/function.array-intersect-key.php)
```php
($func = function ($input, $filter) use (&$func) {
return \is_array($input)
? \array_combine(
\array_keys($filteredInput = \array_intersect_key($input, $filter)),
\array_map(
$func,
$filteredInput,
\array_intersect_key($filter, $input),
),
) : $input;
})(
[
πŸ˜€ => βœ…,
🀠 => ❌,
😎 => [
🍎 => βœ…,
🍐 => ❌,
],
🀑 => [
🍎 => [
🐢 => βœ…,
🐰 => ❌,
],
],
],
[
πŸ˜€ => 1,
πŸ₯Ά => 1,
😎 => [
🍎 => 1,
πŸ‹ => 1,
],
🀑 => [
🍎 => [
🐢 => 1,
🐯 => 1,
],
],
],
);
```
```
[
πŸ˜€ => βœ…,
😎 => [🍎 => βœ…],
🀑 => [🍎 => [🐢 => βœ…]],
]

```

## How To

Requires at least php `7.4`.
```bash
php README.php > README.md
```