Arrays in PHP is a type of data structure that allows us to store multiple elements of similar data type under a single variable thereby saving us the effort of creating a different variable for every data. The arrays are helpful to create a list of elements of similar types, which can be accessed using their index or key. Suppose we want to store five names and print them accordingly. This can be easily done by the use of five different string variables. But if instead of five, the number rises to a hundred, then it would be really difficult for the user or developer to create so many different variables. Here array comes into play and helps us to store every element within a single variable and also allows easy access using an index or a key. An array is created using an array() function in PHP.
In PHP, there are three types of arrays:
- Indexed arrays – Arrays with numeric index
- Associative arrays – Arrays with named keys
- Multidimensional arrays – Arrays containing one or more arrays
<?php $cars=array("mango","banana","orange"); // Index Array $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); // Associative array $cars=array // Multi-dimensional array ( array("Volvo",100,96), array("BMW",60,59), array("Toyota",110,100) ); ?>
array_change_key_case()
<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
print_r(array_change_key_case($age,CASE_UPPER));
?>
Output: Array ( [PETER] => 35 [BEN] => 37 [JOE] => 43 )
// key changed into capital latter
array_chunk()
The array_chunk() function splits an array into chunks of new arrays.
array_chunk(array, size, preserve_key)
1. <?php
$cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel");
echo "<pre>";
print_r(array_chunk($cars,2));
?>
Output:
Array
(
[0] => Array
(
[0] => Volvo
[1] => BMW
)
[1] => Array
(
[0] => Toyota
[1] => Honda
)
[2] => Array
(
[0] => Mercedes
[1] => Opel
)
)
2. <?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43","Harry"=>"50");
echo "<pre>";
print_r(array_chunk($age,2,true)); // true work for key value showing in array also
?>
Output:
Array
(
[0] => Array
(
[Peter] => 35
[Ben] => 37
)
[1] => Array
(
[Joe] => 43
[Harry] => 50
)
)
array_column() The array_column() function returns the values from a single column in the input array. array_column(array, column_key, index_key); 1. <?php // An array that represents a possible record set returned from a database $a = array( array( 'id' => 5698, 'first_name' => 'Peter', 'last_name' => 'Griffin', ), array( 'id' => 4767, 'first_name' => 'Ben', 'last_name' => 'Smith', ), array( 'id' => 3809, 'first_name' => 'Joe', 'last_name' => 'Doe', ) ); $last_names = array_column($a, 'last_name'); print_r($last_names); ?> Output: Array ( [0] => Griffin [1] => Smith [2] => Doe ) 2. <?php // An array that represents a possible record set returned from a database $a = array( array( 'id' => 5698, 'first_name' => 'Peter', 'last_name' => 'Griffin', ), array( 'id' => 4767, 'first_name' => 'Ben', 'last_name' => 'Smith', ), array( 'id' => 3809, 'first_name' => 'Joe', 'last_name' => 'Doe', ) ); $last_names = array_column($a, 'last_name', 'id'); // id will show at the place of index number print_r($last_names); ?> Output: Array ( [5698] => Griffin [4767] => Smith [3809] => Doe )
array_combine()
1. Create an array by using the elements from one "keys" array and one "values" array:
2. Number of element in both array should be same.
3. Number of array must be two only otherwise it will show error.
<?php
$fname=array("Peter","Ben","Joe");
$age=array("35","37","43");
$c=array_combine($fname,$age);
print_r($c);
?>
Output:
Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
array_count_values()
Count all the values of an array:
array_count_values(array)
<?php
$a=array("A","Cat","Dog","A","Dog");
print_r(array_count_values($a));
?>
Output:
Array ( [A] => 2 [Cat] => 1 [Dog] => 2 ) // 2 values of A, 1 value of Cat & 2 value of Dog
array_diff()
Compare the values of two arrays, and return the differences:
array_diff(array1, array2, array3, ...)
<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"black","g"=>"purple");
$a3=array("a"=>"red","b"=>"black","h"=>"yellow");
$result=array_diff($a1,$a2,$a3);
print_r($result);
?>
Output:
Array ( [b] => green [c] => blue )