• Stars
    star
    103
  • Rank 331,901 (Top 7 %)
  • Language
    PHP
  • Created over 3 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

array_chunk([๐ŸŽ, ๐Ÿ, ๐ŸŠ, ๐Ÿ‹, ๐ŸŒ], 2);
[
    [๐ŸŽ, ๐Ÿ], 
    [๐ŸŠ, ๐Ÿ‹], 
    [๐ŸŒ],
]

array_chunk with preserved keys

array_chunk([๐ŸŽ, ๐Ÿ, ๐ŸŠ, ๐Ÿ‹, ๐ŸŒ], 2, true);
[
    [๐ŸŽ, ๐Ÿ], 
    [2 => ๐ŸŠ, 3 => ๐Ÿ‹], 
    [4 => ๐ŸŒ],
]

array_column

array_column(
    [
        ['people' => ๐Ÿ˜€, 'fruit' => ๐ŸŽ, 'animal' => ๐Ÿถ],
        ['people' => ๐Ÿ˜Ž, 'fruit' => ๐Ÿ, 'animal' => ๐Ÿญ],
        ['people' => ๐Ÿฅถ, 'fruit' => ๐ŸŠ, 'animal' => ๐Ÿฐ],
        ['people' => ๐Ÿคก, 'fruit' => ๐Ÿ‹, 'animal' => ๐ŸฆŠ],
        ['people' => ๐Ÿค , 'fruit' => ๐ŸŒ, 'animal' => ๐Ÿฏ],
    ],
    'fruit'
);
[๐ŸŽ, ๐Ÿ, ๐ŸŠ, ๐Ÿ‹, ๐ŸŒ]

array_column with an index

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

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_combine(
    [๐Ÿ˜€, ๐Ÿ˜Ž, ๐Ÿฅถ, ๐Ÿคก, ๐Ÿค ],
    [๐ŸŽ, ๐Ÿ, ๐ŸŠ, ๐Ÿ‹, ๐ŸŒ]
);
[๐Ÿ˜€ => ๐ŸŽ, ๐Ÿ˜Ž => ๐Ÿ, ๐Ÿฅถ => ๐ŸŠ, ๐Ÿคก => ๐Ÿ‹, ๐Ÿค  => ๐ŸŒ]

array_count_values

array_count_values([๐ŸŽ, ๐ŸŽ, ๐ŸŒ, ๐ŸŒ, ๐ŸŽ]);
[๐ŸŽ => 3, ๐ŸŒ => 2]

array_diff

array_diff(
    [๐ŸŽ, ๐Ÿ, ๐ŸŠ, ๐Ÿ‹, ๐ŸŒ],
    [โŒ, ๐Ÿ], [โŒ, ๐ŸŠ], [โŒ, ๐Ÿ‹]
);
[0 => ๐ŸŽ, 4 => ๐ŸŒ]

array_diff_key

array_diff_key(
    [๐Ÿถ => ๐ŸŽ, ๐Ÿญ => ๐Ÿ, ๐Ÿฐ => ๐ŸŠ, ๐ŸฆŠ => ๐Ÿ‹, ๐Ÿฏ => ๐ŸŒ],
    [๐Ÿถ => โŒ, ๐Ÿญ => โŒ], [๐Ÿฐ => โŒ]
);
[๐ŸฆŠ => ๐Ÿ‹, ๐Ÿฏ => ๐ŸŒ]

array_fill_keys

array_fill_keys([๐ŸŽ, ๐Ÿ, ๐ŸŠ, ๐Ÿ‹, ๐ŸŒ], โœ…);
[๐ŸŽ => โœ…, ๐Ÿ => โœ…, ๐ŸŠ => โœ…, ๐Ÿ‹ => โœ…, ๐ŸŒ => โœ…]

array_fill

array_fill(2, 3, ๐ŸŒ);
[2 => ๐ŸŒ, 3 => ๐ŸŒ, 4 => ๐ŸŒ]

array_filter

array_filter([๐ŸŽ, 0, ๐Ÿ, false, ๐ŸŠ, null, ๐Ÿ‹, '', ๐ŸŒ]);
[0 => ๐ŸŽ, 2 => ๐Ÿ, 4 => ๐ŸŠ, 6 => ๐Ÿ‹, 8 => ๐ŸŒ]

array_filter with callback

array_filter([๐ŸŽ, ๐Ÿ, ๐ŸŽ, ๐Ÿ‹, ๐ŸŒ], fn($โ“) => $โ“ === ๐ŸŽ);
[0 => ๐ŸŽ, 2 => ๐ŸŽ]

array_flip

array_flip([๐Ÿถ => ๐ŸŽ, ๐Ÿญ => ๐Ÿ, ๐Ÿฐ => ๐ŸŽ, ๐ŸฆŠ => ๐Ÿ‹, ๐Ÿฏ => ๐ŸŒ]);
[๐ŸŽ => ๐Ÿฐ, ๐Ÿ => ๐Ÿญ, ๐Ÿ‹ => ๐ŸฆŠ, ๐ŸŒ => ๐Ÿฏ]

array_intersect

array_intersect(
    [๐ŸŽ, ๐Ÿ, ๐ŸŠ, ๐Ÿ‹, ๐ŸŒ],
    [โŒ, ๐ŸŠ, ๐Ÿ], [๐ŸŠ, ๐ŸŒ]
);
[2 => ๐ŸŠ]

array_intersect_key

array_intersect_key(
    [๐Ÿถ => ๐ŸŽ, ๐Ÿญ => ๐Ÿ, ๐Ÿฐ => ๐ŸŠ, ๐ŸฆŠ => ๐Ÿ‹, ๐Ÿฏ => ๐ŸŒ],
    [๐Ÿถ => โŒ, ๐ŸฆŠ => โœ…], [๐ŸฆŠ => โœ…, ๐Ÿฏ => โŒ]
);
[๐ŸฆŠ => ๐Ÿ‹]

array_keys

array_keys(
    [๐Ÿถ => ๐ŸŽ, ๐Ÿญ => ๐Ÿ, ๐Ÿฐ => ๐ŸŠ, ๐ŸฆŠ => ๐Ÿ‹, ๐Ÿฏ => ๐ŸŒ]
);
[๐Ÿถ, ๐Ÿญ, ๐Ÿฐ, ๐ŸฆŠ, ๐Ÿฏ]

array_map

array_map(
    fn($๐Ÿ‘ค, $๐Ÿฝ) => "$๐Ÿ‘ค โค๏ธ $๐Ÿฝ",
    [๐Ÿ˜€, ๐Ÿ˜Ž, ๐Ÿฅถ, ๐Ÿคก],
    [๐ŸŽ, ๐Ÿ, ๐ŸŠ, ๐Ÿ‹, ๐ŸŒ]
);
['๐Ÿ˜€ โค๏ธ ๐ŸŽ', '๐Ÿ˜Ž โค๏ธ ๐Ÿ', '๐Ÿฅถ โค๏ธ ๐ŸŠ', '๐Ÿคก โค๏ธ ๐Ÿ‹', ' โค๏ธ ๐ŸŒ']

array_map with null callback

array_map(
    null,
    [๐Ÿ˜€, ๐Ÿ˜Ž, ๐Ÿฅถ, ๐Ÿคก, ๐Ÿค ],
    [๐ŸŽ, ๐Ÿ, ๐ŸŠ, ๐Ÿ‹],
    [๐Ÿถ, ๐Ÿญ, ๐Ÿฐ, ๐ŸฆŠ, ๐Ÿฏ]
);
[
    [๐Ÿ˜€, ๐ŸŽ, ๐Ÿถ], 
    [๐Ÿ˜Ž, ๐Ÿ, ๐Ÿญ], 
    [๐Ÿฅถ, ๐ŸŠ, ๐Ÿฐ], 
    [๐Ÿคก, ๐Ÿ‹, ๐ŸฆŠ], 
    [๐Ÿค , null, ๐Ÿฏ],
]

array_merge

array_merge(
    [๐Ÿถ => ๐ŸŽ, ๐Ÿญ => ๐Ÿ, ๐Ÿฐ => โŒ],
    [๐Ÿฐ => ๐ŸŠ, ๐ŸฆŠ => โŒ, ๐Ÿฏ => ๐ŸŒ],
    [๐ŸฆŠ => ๐Ÿ‹],
);
[๐Ÿถ => ๐ŸŽ, ๐Ÿญ => ๐Ÿ, ๐Ÿฐ => ๐ŸŠ, ๐ŸฆŠ => ๐Ÿ‹, ๐Ÿฏ => ๐ŸŒ]

Union operator

[๐Ÿถ => ๐ŸŽ, ๐Ÿญ => ๐Ÿ, ๐Ÿฐ => ๐ŸŠ] +
    [๐Ÿฐ => โŒ, ๐ŸฆŠ => ๐Ÿ‹, ๐Ÿฏ => ๐ŸŒ] +
    [๐ŸฆŠ => โŒ];
[๐Ÿถ => ๐ŸŽ, ๐Ÿญ => ๐Ÿ, ๐Ÿฐ => ๐ŸŠ, ๐ŸฆŠ => ๐Ÿ‹, ๐Ÿฏ => ๐ŸŒ]

array_merge with integer keys

array_merge([๐ŸŽ, ๐Ÿ], [๐ŸŠ, ๐ŸŒ], [๐Ÿ‹]);
[๐ŸŽ, ๐Ÿ, ๐ŸŠ, ๐ŸŒ, ๐Ÿ‹]

Union operator with integer keys

[๐ŸŽ, ๐Ÿ] + [๐ŸŠ, ๐ŸŒ] + [๐Ÿ‹];
[๐ŸŽ, ๐Ÿ]

array_pad

array_pad([๐ŸŽ, ๐Ÿ], 5, ๐ŸŒ);
[๐ŸŽ, ๐Ÿ, ๐ŸŒ, ๐ŸŒ, ๐ŸŒ]

array_reverse

array_reverse([๐ŸŽ, ๐Ÿ, ๐ŸŠ, ๐Ÿ‹, ๐ŸŒ]);
[๐ŸŒ, ๐Ÿ‹, ๐ŸŠ, ๐Ÿ, ๐ŸŽ]

array_reverse with preserved keys

array_reverse([๐ŸŽ, ๐Ÿ, ๐ŸŠ, ๐Ÿ‹, ๐ŸŒ], true);
[4 => ๐ŸŒ, 3 => ๐Ÿ‹, 2 => ๐ŸŠ, 1 => ๐Ÿ, 0 => ๐ŸŽ]

array_slice

array_slice([๐ŸŽ, ๐ŸŒ, ๐ŸŒ, ๐ŸŒ, ๐ŸŽ], 1, 3);
[๐ŸŒ, ๐ŸŒ, ๐ŸŒ]

array_slice with preserved keys

array_slice([๐ŸŽ, ๐ŸŒ, ๐ŸŒ, ๐ŸŒ, ๐ŸŽ], 1, 3, true);
[1 => ๐ŸŒ, 2 => ๐ŸŒ, 3 => ๐ŸŒ]

array_unique

array_unique([๐ŸŽ, ๐ŸŽ, ๐ŸŒ, ๐ŸŒ, ๐ŸŽ]);
[0 => ๐ŸŽ, 2 => ๐ŸŒ]

array_values

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

!(
    ($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

!($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

!($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

!($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

array_map(
        null,
    ...[
        [๐Ÿ˜€, ๐ŸŽ, ๐Ÿถ],
        [๐Ÿ˜Ž, ๐Ÿ, ๐Ÿญ],
        [๐Ÿฅถ, ๐ŸŠ, ๐Ÿฐ],
        [๐Ÿคก, ๐Ÿ‹, ๐ŸฆŠ],
        [๐Ÿค , ๐ŸŠ, ๐Ÿฏ],
    ]
);
[
    [๐Ÿ˜€, ๐Ÿ˜Ž, ๐Ÿฅถ, ๐Ÿคก, ๐Ÿค ], 
    [๐ŸŽ, ๐Ÿ, ๐ŸŠ, ๐Ÿ‹, ๐ŸŠ], 
    [๐Ÿถ, ๐Ÿญ, ๐Ÿฐ, ๐ŸฆŠ, ๐Ÿฏ],
]

How To

Requires at least php 7.4.

php README.php > README.md