OdinSchool OdinSchool
Arrays in PHP | Learning Hub

Arrays in PHP | Learning Hub

An array is a data structure that stores one or more similar types of values in a single value. For example, if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 lengths.

There are three different kinds of arrays and each array value is accessed using an ID which is called array index.

  • Indexed array − An array with a numeric index. Values are stored and accessed in a linear way.
  • Associative array − An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.
  • Multidimensional array − An array containing one or more arrays and values are accessed using multiple indices.

PHP Indexed Array

PHP index is represented by a number that starts from 0. We can store number, string,s and objects in the PHP array. All PHP array elements are assigned to an index number by default.

There are two ways to define an indexed array:

1st way:

$season=array("summer","winter","spring","autumn"); 

2nd way:

$season[0]="summer";  
$season[1]="winter";  
$season[2]="spring";  
$season[3]="autumn";

Example 1:

<?
$season = array("Summer", "winter", "spring", "autumn");
echo "Seasons are: " . $season[0] . ", " . $season[1] . ", ". $season[2] . " and " . $season[3] . ".";
?>

Output:

Season are: summer, winter, spring and autumn

Example 2:

<?
$season[0]="summer";  
$season[1]="winter";  
$season[2]="spring";  
$season[3]="autumn";  
echo "Season are: $season[0], $season[1], $season[2] and $season[3]"; 
?>

Output:

Season are: summer, winter, spring and autumn

PHP Associative Array

We can associate name with each array elements in PHP using => symbol.

There are two ways to define associative array:

1st way:

$salary=array("Sonoo"=>"350000","John"=>"450000","Kartik"=>"200000");  

2nd way:

$salary["Sonoo"]="350000";  
$salary["John"]="450000";  
$salary["Kartik"]="200000";

Example:

<?  
$salary=array("Sonoo"=>"350000","John"=>"450000","Kartik"=>"200000");    
echo "Sonoo salary: ".$salary["Sonoo"]."
";  
echo "John salary: ".$salary["John"]."
";  
echo "Kartik salary: ".$salary["Kartik"]."
";  
?>

Output:

Sonoo salary: 350000
John salary: 450000
Kartik salary: 200000

Example:

<?  
$salary["Sonoo"]="350000";    
$salary["John"]="450000";    
$salary["Kartik"]="200000";    
echo "Sonoo salary: ".$salary["Sonoo"]."
";  
echo "John salary: ".$salary["John"]."
";  
echo "Kartik salary: ".$salary["Kartik"]."
";  
?>

Output:

Sonoo salary: 350000
John salary: 450000
Kartik salary: 200000

PHP Multidimensional Array

PHP multidimensional array is also known as array of arrays. It allows you to store tabular data in an array. PHP multidimensional array can be represented in the form of matrix which is represented by row * column.

$emp = array  
(  
array(1,"sonoo",400000),  
array(2,"john",500000),  
array(3,"rahul",300000)  
);

A multi-dimensional array each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index.

Example:

<?   
$emp = array  
(  
  array(1,"sonoo",400000),  
  array(2,"john",500000),  
  array(3,"rahul",300000)  
);  
for ($row = 0; $row < 3; $row++) {  
  for ($col = 0; $col < 3; $col++) {  
    echo $emp[$row][$col]."  ";  
  }   
}  
?>

Output:

sonoo 400000
john 500000
rahul 300000

PHP Array Functions

PHP provides various array functions to access and manipulate the elements of array. The important PHP array functions are given below.

PHP array() function

PHP array() function creates and returns an array. It allows you to create indexed, associative and multidimensional arrays.

Syntax:

array array ([ mixed $... ] )  

PHP array_change_key_case() function

PHP array_change_key_case() function changes the case of all key of an array.

Note: It changes case of key only.

Syntax:

array array_change_key_case ( array $array [, int $case = CASE_LOWER ] )  

PHP array_chunk() function

PHP array_chunk() function splits array into chunks. By using array_chunk() method, you can divide array into many parts.

Syntax:

array array_chunk (array $array , int $size [, bool $preserve_keys = false ] )  

PHP count() function

PHP count() function counts all elements in an array.

Syntax:

int count ( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )  

PHP sort() function

PHP sort() function sorts all the elements in an array.

Syntax:

bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )  

PHP array_reverse() function

PHP array_reverse() function returns an array containing elements in reversed order.

Syntax:

array array_reverse ( array $array [, bool $preserve_keys = false ] )  

PHP array_search() function

PHP array_search() function searches the specified value in an array. It returns key if search is successful.

Syntax:

mixed array_search ( mixed $needle , array $haystack [, bool $strict = false ] )  

PHP array_intersect() function

PHP array_intersect() function returns the intersection of two array. In other words, it returns the matching elements of two arrays.

Syntax:

array array_intersect ( array $array1 , array $array2 [, array $... ] )