This tutorial is created to give an abstract idea about commonly used PHP array function with examples. Through this tutorial you’ll be able to know, how to use array functions to make code more short and simple.
Note
I’ll be only covering some of the basic array functions, Which were frequently used by PHP developers. You can refer all the array functions from the official PHP Documentation. Feel free to share your opinion in the comment section.
Creating an Array in PHP
PHP array is created using an array() function. There are basically three types of arrays in PHP
- Indexed arrays – An array with a numeric index.
- Associative arrays – An array with named keys.
- Multidimensional arrays – An array containing one or more arrays.
To create an indexed array, add array values inside the parentheses, separated by commas.
$randomNames = array("Peace","Love","BH","Bishrul","Haq");
OR
$randomNames = array();
$randomNames[0] = "Peace";
$randomNames[1] = "Love";
$randomNames[2] = "BH";
$randomNames[3] = "Bishrul";
$randomNames[4] = "Haq";
Let’s look into some of the PHP functions in detail.
- List()
List function is used to Assign variables as if they were an array. It helps to insert without assigning values to each array index.
$names = array("Dev","BH","Peace");
// without list function()
$one = $names[0];
$two = $names[1];
$three = $names[2];
// with list function()
list($one,$two,$three) = $names;
echo 'one : '. $one.' two : '. $two.' three : '. $three;
Output
Array ( [0] => BH [1] => is [2] => Carm )
- preg_slit() or explode()
These two functions are used to split a string by a given string. Which helps to skip string, if you don’t need them to be shown.
$words = 'BH|is|Carm';
$array = explode('|', $words);
// exploded words are displayed in the array
print_r($array);
Output
one : Dev two : BH three : Peace
- extract()
extract() function is used to export an associative array to variables. For every array element, a variable will be created with the name of a key and value as a value of the element.
$details = [
'name' => 'bishrul',
'age' => '24',
'color' => 'fair',
];
extract($details);
echo("$name $age $color");
Output
bishrul 24 fair
- compact()
It’s the exact opposite of explode() function, Which convert the given variables into an associative array.
$name = 'bishrul';
$age = '24';
$color = 'fair';
$array = compact('name', 'age', 'color');
print_r($array);
Output
Array ( [name] => bishrul [age] => 24 [color] => fair )
- array_combine()
Which creates an array by using one array for array keys and another for its array values.
$keys = ['name', 'age', 'color'];
$values = ['bishrul', '24', 'fair'];
$details = array_combine($keys, $values);
print_r($details);
Output
Array ( [name] => bishrul [age] => 24 [color] => fair )
- array_merge()
To merge two or more arrays into one array array_merge() is used. values of arrays will be merged together, and values with the same keys will be overwritten with the last array.
$array1 = ['one' => '1', 'two' => '2', 'three' => '3'];
$array2 = ['one' => '11', 'four' => '4', 'three' => '33'];
$merge = array_merge($array1, $array2);
print_r($merge);
Output
Array ( [one] => 11 [two] => 2 [three] => 33 [four] => 4 )
- array_sum() & array_product()
array_sum() is used to get the sum of given array values and array_product() to multiply the given array values.
$values = [4, 4, 3, 1, 5];
echo('Sum :'.array_sum($values).' Product :'. array_product($values));
Output
Sum :17 Product :240
If you like the article please share with others and drop your ideas and suggestions at the comment section.