๐
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
๐ ) ๐คฏ
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