Skip to content

mckey/php-emoji

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Php array functions… with Emojis πŸ™‚

Do you know Php array functions? 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([🍎, 🍐, 🍊, πŸ‹, 🍌], 2);
[
    [🍎, 🍐], 
    [🍊, πŸ‹], 
    [🍌],
]
array_chunk([🍎, 🍐, 🍊, πŸ‹, 🍌], 2, true);
[
    [🍎, 🍐], 
    [2 => 🍊, 3 => πŸ‹], 
    [4 => 🍌],
]
array_column(
    [
        ['people' => πŸ˜€, 'fruit' => 🍎, 'animal' => 🐢],
        ['people' => 😎, 'fruit' => 🍐, 'animal' => 🐭],
        ['people' => πŸ₯Ά, 'fruit' => 🍊, 'animal' => 🐰],
        ['people' => 🀑, 'fruit' => πŸ‹, 'animal' => 🦊],
        ['people' => 🀠, 'fruit' => 🍌, 'animal' => 🐯],
    ],
    'fruit'
);
[🍎, 🍐, 🍊, πŸ‹, 🍌]
array_column(
    [
        ['people' => πŸ˜€, 'fruit' => 🍎, 'animal' => 🐢],
        ['people' => 😎, 'fruit' => 🍐, 'animal' => 🐭],
        ['people' => πŸ₯Ά, 'fruit' => 🍊, 'animal' => 🐰],
        ['people' => 🀑, 'fruit' => πŸ‹, 'animal' => 🦊],
        ['people' => 🀠, 'fruit' => 🍌, 'animal' => 🐯],
    ],
    'fruit',
    'people'
);
[πŸ˜€ => 🍎, 😎 => 🍐, πŸ₯Ά => 🍊, 🀑 => πŸ‹, 🀠 => 🍌]
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(
    [πŸ˜€, 😎, πŸ₯Ά, 🀑, 🀠],
    [🍎, 🍐, 🍊, πŸ‹, 🍌]
);
[πŸ˜€ => 🍎, 😎 => 🍐, πŸ₯Ά => 🍊, 🀑 => πŸ‹, 🀠 => 🍌]
array_count_values([🍎, 🍎, 🍌, 🍌, 🍎]);
[🍎 => 3, 🍌 => 2]
array_diff(
    [🍎, 🍐, 🍊, πŸ‹, 🍌],
    [❌, 🍐], [❌, 🍊], [❌, πŸ‹]
);
[0 => 🍎, 4 => 🍌]
array_diff_key(
    [🐢 => 🍎, 🐭 => 🍐, 🐰 => 🍊, 🦊 => πŸ‹, 🐯 => 🍌],
    [🐢 => ❌, 🐭 => ❌], [🐰 => ❌]
);
[🦊 => πŸ‹, 🐯 => 🍌]
array_fill_keys([🍎, 🍐, 🍊, πŸ‹, 🍌], βœ…);
[🍎 => βœ…, 🍐 => βœ…, 🍊 => βœ…, πŸ‹ => βœ…, 🍌 => βœ…]
array_fill(2, 3, 🍌);
[2 => 🍌, 3 => 🍌, 4 => 🍌]
array_filter([🍎, 0, 🍐, false, 🍊, null, πŸ‹, '', 🍌]);
[0 => 🍎, 2 => 🍐, 4 => 🍊, 6 => πŸ‹, 8 => 🍌]
array_filter([🍎, 🍐, 🍎, πŸ‹, 🍌], fn($❓) => $❓ === 🍎);
[0 => 🍎, 2 => 🍎]
array_flip([🐢 => 🍎, 🐭 => 🍐, 🐰 => 🍎, 🦊 => πŸ‹, 🐯 => 🍌]);
[🍎 => 🐰, 🍐 => 🐭, πŸ‹ => 🦊, 🍌 => 🐯]
array_intersect(
    [🍎, 🍐, 🍊, πŸ‹, 🍌],
    [❌, 🍊, 🍐], [🍊, 🍌]
);
[2 => 🍊]
array_intersect_key(
    [🐢 => 🍎, 🐭 => 🍐, 🐰 => 🍊, 🦊 => πŸ‹, 🐯 => 🍌],
    [🐢 => ❌, 🦊 => βœ…], [🦊 => βœ…, 🐯 => ❌]
);
[🦊 => πŸ‹]
array_keys(
    [🐢 => 🍎, 🐭 => 🍐, 🐰 => 🍊, 🦊 => πŸ‹, 🐯 => 🍌]
);
[🐢, 🐭, 🐰, 🦊, 🐯]
array_map(
    fn($πŸ‘€, $🍽) => "$πŸ‘€ ❀️ $🍽",
    [πŸ˜€, 😎, πŸ₯Ά, 🀑],
    [🍎, 🍐, 🍊, πŸ‹, 🍌]
);
['πŸ˜€ ❀️ 🍎', '😎 ❀️ 🍐', 'πŸ₯Ά ❀️ 🍊', '🀑 ❀️ πŸ‹', ' ❀️ 🍌']
array_map(
    null,
    [πŸ˜€, 😎, πŸ₯Ά, 🀑, 🀠],
    [🍎, 🍐, 🍊, πŸ‹],
    [🐢, 🐭, 🐰, 🦊, 🐯]
);
[
    [πŸ˜€, 🍎, 🐢], 
    [😎, 🍐, 🐭], 
    [πŸ₯Ά, 🍊, 🐰], 
    [🀑, πŸ‹, 🦊], 
    [🀠, null, 🐯],
]
array_merge(
    [🐢 => 🍎, 🐭 => 🍐, 🐰 => ❌],
    [🐰 => 🍊, 🦊 => ❌, 🐯 => 🍌],
    [🦊 => πŸ‹],
);
[🐢 => 🍎, 🐭 => 🍐, 🐰 => 🍊, 🦊 => πŸ‹, 🐯 => 🍌]
[🐢 => 🍎, 🐭 => 🍐, 🐰 => 🍊] +
    [🐰 => ❌, 🦊 => πŸ‹, 🐯 => 🍌] +
    [🦊 => ❌];
[🐢 => 🍎, 🐭 => 🍐, 🐰 => 🍊, 🦊 => πŸ‹, 🐯 => 🍌]
array_merge([🍎, 🍐], [🍊, 🍌], [πŸ‹]);
[🍎, 🍐, 🍊, 🍌, πŸ‹]
[🍎, 🍐] + [🍊, 🍌] + [πŸ‹];
[🍎, 🍐]
array_pad([🍎, 🍐], 5, 🍌);
[🍎, 🍐, 🍌, 🍌, 🍌]
array_reverse([🍎, 🍐, 🍊, πŸ‹, 🍌]);
[🍌, πŸ‹, 🍊, 🍐, 🍎]
array_reverse([🍎, 🍐, 🍊, πŸ‹, 🍌], true);
[4 => 🍌, 3 => πŸ‹, 2 => 🍊, 1 => 🍐, 0 => 🍎]
array_slice([🍎, 🍌, 🍌, 🍌, 🍎], 1, 3);
[🍌, 🍌, 🍌]
array_slice([🍎, 🍌, 🍌, 🍌, 🍎], 1, 3, true);
[1 => 🍌, 2 => 🍌, 3 => 🍌]
array_unique([🍎, 🍎, 🍌, 🍌, 🍎]);
[0 => 🍎, 2 => 🍌]
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! πŸ˜…

!(
    ($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],
]
!($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' => 🐯]
!($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' => 🐯],
]
!($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(
        null,
    ...[
        [πŸ˜€, 🍎, 🐢],
        [😎, 🍐, 🐭],
        [πŸ₯Ά, 🍊, 🐰],
        [🀑, πŸ‹, 🦊],
        [🀠, 🍊, 🐯],
    ]
);
[
    [πŸ˜€, 😎, πŸ₯Ά, 🀑, 🀠], 
    [🍎, 🍐, 🍊, πŸ‹, 🍊], 
    [🐢, 🐭, 🐰, 🦊, 🐯],
]

How To

Requires at least php 7.4.

php README.php > README.md

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%