Categories
PHP

PHP Array Functions and their usage with examples

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

  1. Indexed arrays – An array with a numeric index.
  2. Associative arrays – An array with named keys.
  3. 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");
Code language: PHP (php)

OR

$randomNames = array();

$randomNames[0] = "Peace";
$randomNames[1] = "Love";
$randomNames[2] = "BH";
$randomNames[3] = "Bishrul";
$randomNames[4] = "Haq";
Code language: PHP (php)

Let’s look into some of the PHP functions in detail.

  1. 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;
Code language: PHP (php)

Output

Array ( [0] => BH [1] => is [2] => Carm )
Code language: PHP (php)
  1. 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);
Code language: PHP (php)

Output

one : Dev two : BH three : Peace
  1. 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");
Code language: PHP (php)

Output

bishrul 24 fair
  1. 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);
Code language: PHP (php)

Output

Array ( [name] => bishrul [age] => 24 [color] => fair )
Code language: PHP (php)
  1. 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);
Code language: PHP (php)

Output

Array ( [name] => bishrul [age] => 24 [color] => fair )
Code language: PHP (php)
  1. 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);
Code language: PHP (php)

Output

Array ( [one] => 11 [two] => 2 [three] => 33 [four] => 4 )
Code language: PHP (php)
  1. 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)); 
Code language: PHP (php)

Output

Sum :17 Product :240
Code language: CSS (css)

If you like the article please share with others and drop your ideas and suggestions at the comment section.

Leave a Reply

Your email address will not be published.