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! π
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_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(
[πΆ => π, π => π, π° => π, π¦ => π, π― => π]
);
[π, π, π, π, π]
These are what I call array tipsΒ©:
- Single statement
- No anonymous function (but short arrow functions are allowed π)
- π€―
!(
($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]
Requires at least php 7.4
.
php README.php > README.md