Array in PHP

An array is an ordered collection of elements. Each element has a value, and is identified by a key. Each array has its own unique keys. An array is a special variable, which can store multiple values in one single variable.

It can hold all your variable values under a single name and you can access the values by referring to the array name.

The keys can be either integer numbers or strings.

Key Value of Array in PHP

In PHP, the array() function is used to create an array.

Advantage of PHP Array

  • Less Code: We don't need to define multiple variables.
  • Easy to traverse: By the help of single loop, we can traverse all the elements of an array.
  • Sorting: We can sort the elements of array.

Types of Array

In PHP, there are three types of arrays.

  1. Indexed/Numeric Arrays - Arrays with a numeric index
  2. Associative Arrays - Arrays with named keys
  3. Multidimensional Arrays - Arrays containing one or more arrays

1. Indexed/Numeric Arrays

Indexed array is represented by number which starts from 0. We can store number, string and object in the PHP array. All PHP array elements are assigned to an index number by default.

Syntax:

$ArrayName[] = "value";

There are two ways to define indexed array.

The index can be assigned automatically (index always starts at 0), like Initializing indexed array.

$programmings = array("PHP", "Java", "Python", "C");

Also index can be assigned manually.

$programmings[0] = "PHP";
$programmings[1] = "Java";
$programmings[2] = "Python";
$programmings[3] = "Python";

Example

<?php
	$programmings = array("PHP", "Java", "Python", "C");
	echo "Programmings are: $programmings[0], $programmings[1], $programmings[2] and $programmings[3]";
?> 

2. Associative Arrays

Associative arrays are arrays that use named keys that you assign to them.

Syntax

$ArrayName = array( Key1=>Value1, Key2=>Value2, …, KeyN=>ValueN)

There are two ways to create an associative array.

First Way:

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

Second Way (key=Value):

$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";

Example

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

3. Multidimensional Arrays

This is a rather advanced PHP stuff, but for the sake of this tutorial, just understand what a multidimensional array is. Basically, it is an arrays the elements of which are other arrays.

For example, a three-dimensional array is an array of arrays of arrays.

An example of this kind of array would be:

<?php
	$socialNetowrks = array (
		array("Facebook", "Feb", 21),
		array("Twitter", "Dec", 2),
		array("Instagram", "Aug", 15)
	);
?>

Array Iteration

Array iteration methods

  • Using for Loop
  • Using foreach Loop
  • Using while Loop with each() Function
  • Using array_walk() with a callback function
  • Using array_map() with a Callback Function
  • Using array_reduce()

Finding the Length of an Array in PHP

Using count() function you can find length of an array in php.

Example

<?php
	$student = array("Harry", "Varsha", "Gaurav");
	echo "Length of Array: ";
	echo count($student);
?>

Array Functions

FunctionDescription
array()Creates an array
array_combine()Creates an array by using the elements from one "keys" array and one "values" array
array_count_values()Counts all the values of an array
array_diff()Compare arrays, and returns the differences (compare values only)
array_diff_assoc()Compare arrays, and returns the differences (compare keys and values)
array_diff_key()Compare arrays, and returns the differences (compare keys only
array_fill()Fills an array with values
array_fill_keys()Fills an array with values, specifying keys
array_filter()Filters the values of an array using a callback function