Master Essential PHP Array Functions for Cleaner Code
This guide walks PHP developers through eight essential array functions—array_map, array_walk, array_reduce, array_filter, array_merge, array_keys, array_values, array_unique, and array_combine—explaining their purpose, usage, and providing clear code examples to help manage and transform data efficiently.
If you want to become a proficient PHP developer, mastering array functions is essential because arrays are used for temporary storage, organization, and processing of data before persisting to a database.
1. array_map
The array_map function applies a given callback to each element of one or more arrays and returns a new array with the results, preserving the original keys.
$numbers = [1, 2, 3, 4];
$squaredNumbers = array_map(
function($n) {
return $n * $n;
},
$numbers
);
print_r($squaredNumbers);
// Output: [1, 4, 9, 16]Tip: If you only need to modify the existing array without creating a new one, use array_walk , which applies the callback directly to each element.
2. array_walk
array_walkapplies a callback to each element of an array, modifying the array in place and returning no value.
$numbers = [1, 2, 3, 4];
array_walk($numbers, function(&$n) {
$n *= $n;
});
print_r($numbers);
// Output: [1, 4, 9, 16]3. array_reduce
array_reducereduces an array to a single value by iteratively applying a callback that receives the accumulated value and the current item.
$numbers = [1, 2, 3, 4];
$sum = array_reduce($numbers, function($carry, $item) {
return $carry + $item;
}, 0);
echo $sum; // Output: 104. array_filter
array_filterreturns a new array containing only the elements for which the callback returns true. The callback can also receive each element's key.
$numbers = [1, 2, 3, 4, 5];
$evenNumbers = array_filter($numbers, function($n) {
return $n % 2 === 0;
});
print_r($evenNumbers);
// Output: [2, 4]5. array_merge
array_mergecombines one or more arrays into a single array, overwriting values with the same string keys from later arrays.
$array1 = ["a" => "pomme", "b" => "banane"];
$array2 = ["a" => "ananas", "c" => "citron"];
$result = array_merge($array1, $array2);
print_r($result);
// Output: ["a" => "ananas", "b" => "banane", "c" => "citron"]6. array_keys
array_keysreturns all the keys of an array. For checking the existence of a specific key, array_key_exists is more direct.
$array = ['nom' => 'Jean', 'âge' => 30];
if (array_key_exists('nom', $array)) {
echo 'La clé "nom" existe dans le tableau';
}7. array_values
array_valuesreturns all the values from an array, re-indexed numerically.
$array = ['a' => 'pomme', 'b' => 'banane', 'c' => 'citron'];
$values = array_values($array);
print_r($values);
// Output: ['pomme', 'banane', 'citron']8. array_unique
array_uniqueremoves duplicate values from an array, preserving the first occurrence of each value.
$array = ['pomme', 'banane', 'pomme', 'citron'];
$uniqueArray = array_unique($array);
print_r($uniqueArray);
// Output: ['pomme', 'banane', 'citron']9. array_combine
array_combinecreates an array by using one array for keys and another for values, useful for pairing related data sets.
$keys = ['nom', 'prénom', 'âge'];
$values = ['Dupont', 'Jean', 30];
$resultat = array_combine($keys, $values);
print_r($resultat);
// Output:
// Array
// (
// [nom] => Dupont
// [prénom] => Jean
// [âge] => 30
// )Knowing how to manipulate arrays is a fundamental skill for any PHP developer; mastering these functions enables more efficient data handling and cleaner, more maintainable code.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
