PHP Tutorial - PHP Array Operator






You can create and manage arrays using square brackets []. [] can both create arrays and add elements to the end of an array.

Syntax for array operator []

Array operator has the following syntax.

$arratName[] = "new Value";

or

$arratName[index] = "new Value";

or

$arratName["key"] = "new Value";

or

$arratName[index]; // get value by index

or

$arratName["key"]; // get value by key

Example to use PHP Array Operator

The following code uses the array operator to create and indexed array.

<?PHP
$array[] = "Foo"; 
$array[] = "Bar"; 
$array[] = "java2s.com"; 
var_dump($array); 
?>

The code above generates the following result.





Example - Create Associative Arrays with Array Operator

For non-default indices, we can place our key inside the square brackets, like this:

<?PHP
$array["a"] = "Foo"; 
$array["b"] = "Bar"; 
$array["c"] = "Baz"; 
var_dump($array); 
?>

The code above generates the following result.

PHP Access Array Element

By using array operator [] we can access elements in an array.





Syntax to access array element

For indexed array we can use number index to access array element

$arrayName[0]
$arrayName[1]

For associative arrays we can put element name to the array operator[]

$arrayName["key1"]
$arrayName["key2"]

Example - Access indexed array elements

Access indexed array elements

<?PHP
$authors = array( "Java", "PHP", "CSS", "HTML" ); 
$myAuthor = $authors[0];      // $myAuthor contains "Java" 
$anotherAuthor = $authors[1]; // $anotherAuthor contains "PHP" 
print($myAuthor);
print($anotherAuthor);
?>

The code above generates the following result.

Example - Access elements from associative arrays

Access elements from associative arrays

<?PHP
$myBook = array( "title" =>  "Learn PHP from java2s.com", 
                 "author" =>  "java2s.com", 
                 "pubYear" =>  2000 ); 

$myTitle = $myBook["title"];    // $myTitle contains "Learn PHP from java2s.com" 
$myAuthor = $myBook["author"];  // $myAuthor contains "Java"   
print($myTitle);
print($myAuthor);
?>

The code above generates the following result.

Example - Put expression into array operator[]

We don't have to use literal values within the square brackets; you can use any expression, as long as it evaluates to an integer or string.

<?PHP
$authors = array( "Java", "PHP", "CSS", "HTML" ); 
$pos = 2; 
echo $authors[$pos + 1]; // Displays "HTML"   
?>

The code above generates the following result.

PHP Change Array Element

We can change values using the same techniques, [].

To change indexed array.

$arrayName[0] = newValue
$arrayName[1] = newValue

To change associative arrays

$arrayName["key0"] = newValue
$arrayName["key1"] = newValue

The following code changes the value of the third element in an indexed array from "CSS" to "Python".

<?PHP
$authors = array( "Java", "PHP", "CSS", "HTML" ); 
$authors[2] = "Python";   
var_dump($authors); 
?>

The code above generates the following result.

Example - Add to the last element

What if you wanted to add a fifth author? You can just create a new element with an index of 4, as follows:

<?PHP
$authors = array( "Java", "PHP", "CSS", "HTML" ); 
$authors[4] = "Ruby";   
var_dump($authors); 
?>

The code above generates the following result.

Example - Append to the array end

There's an even easier way to add a new element to an array - simply use square brackets with no index:

<?PHP
$authors = array( "Java", "PHP", "CSS", "HTML" ); 
$authors[] = "Ruby";   
var_dump($authors); 
?>

The code above generates the following result.

When you do this, PHP knows that you want to add a new element to the end of the array, and it automatically assigns the next available index to the element.

PHP Create Array using Square Bracket

We can create an array from scratch simply by creating its elements using the square bracket syntax.

For indexed arrays

$arrayName[] = new value
$arrayName[] = new value

For associative arrays

$arrayName["new Key"] = new value
$arrayName["new Key2"] = new value2

The following three examples all produce exactly the same array:

Creating an array using the array() construct

$authors1 = array( "Java", "PHP", "CSS", "HTML" );

Creating the same array using [] and numeric indices

$authors2[0] = "Java"; 
$authors2[1] = "PHP"; 
$authors2[2] = "CSS"; 
$authors2[3] = "HTML"; 

Creating the same array using the empty [] syntax

$authors3[] = "Java"; 
$authors3[] = "PHP"; 
$authors3[] = "CSS"; 
$authors3[] = "HTML";   

Example - clear array first and then create array using square bracket

Since the square bracket can also append value to the end of an array. We can avoid append value to the existing array by clear the existing array first with array() function.

$authors = array();   

This creates an array with no elements (an empty array). We can then go ahead and add elements later:

 
$authors[] = "Java"; 
$authors[] = "PHP"; 
$authors[] = "CSS"; 
$authors[] = "HTML";  

Example - create associative array using square bracket

Add and change elements of associative arrays using square bracket syntax. Here an associative array is populated in two ways: first using the array() construct, and second using the square bracket syntax:

 
// Creating an associative array using the array() construct 
$myBook = array( "title" =>  "Learn PHP from java2s.com", 
                "author" =>  "java2s.com", 
                "pubYear" =>  2000 ); 

// Creating the same array using [] syntax 
$myBook = array(); 
$myBook["title"] = "Learn PHP from java2s.com"; 
$myBook["author"] = "java2s.com"; 
$myBook["pubYear"] = 2000;  

Changing elements of associative arrays works in a similar fashion to indexed arrays:

  
$myBook["title"] = "java from java2s.com"; 
$myBook["pubYear"] = 1952;