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_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(
[πΆ => π, π => π, π° => π, π¦ => π, π― => π]
);
[π, π, π, π, π]
Requires at least php 7.4
.
php README.php > README.md