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";

$arratName[index] = "new Value";

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

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

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

Example to use PHP Array Operator

			$array[] = "php"; 
			$array[] = "mysql"; 
			$array[] = "android"; 

Example - Create Associative Arrays with Array Operator

			$array["a"] = "php"; 
			$array["b"] = "mysql"; 
			$array["c"] = "android"; 

PHP Access Array Element

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


Access indexed array elements

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

Access elements from associative arrays

			$myBook = array( "title" =>  "Learn PHP", 
			                 "author" =>  "", 
			                 "pubYear" =>  2018 ); 

			$myTitle = $myBook["title"];    // $myTitle contains "Learn PHP from" 
			$myAuthor = $myBook["author"];  // $myAuthor contains "Java"   

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.

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

PHP Change Array Element

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

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

Add to the last element

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

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:

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

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

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"; 

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";

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", 
			                "author" =>  "", 
			                "pubYear" =>  2018 ); 

			// Creating the same array using [] syntax 
			$myBook = array(); 
			$myBook["title"] = "Learn PHP"; 
			$myBook["author"] = ""; 
			$myBook["pubYear"] = 2018;

PHP Tutorials






Echo and Print Statement

Data Types



Number Convert

PHP String

php variable substitution in string

php string element

php string function

php addcslashes function

php addslashes function

php get magic quotes gpc function

php get meta tags function

php urlencode function

PHP $ and $$ Variables

PHP constant

PHP Magic Constants

PHP Static Variable

PHP Super Globals

PHP Globals

PHP Server

PHP reserved variable get

PHP reserved variable post

PHP reserved variable FILES

PHP reserved variable Cookies

PHP reserved variable Session

PHP reserved variable Request

PHP reserved variable environment

PHP Operator

PHP Arithmetic Operator

PHP Comparison Operator

PHP Incrementing and Decrementing Operators

PHP Identical Operators

PHP Logical Operators

PHP String Operators

PHP Ternary Operators

PHP Execution Operators

PHP Operator Precedence

PHP Array Operator

PHP Class

PHP Class Inheritance

PHP constructor and destructor

PHP abstract class

PHP class access control

PHP statement

PHP If statement

PHP else statement

PHP else if statement

PHP control structure statement

PHP while loop

PHP do while loop

PHP for loop

PHP for each loop

PHP break statement

PHP continue statement

PHP declare statement

PHP return statement

PHP require statement

PHP include

PHP require once

PHP include once

PHP goto statement

PHP function

PHP function parameter

PHP function scope

PHP array

PHP array operator

PHP array element loop

PHP array foreach loop

PHP array multidimensional

PHP html forms

PHP get and post method

form redirect

file inclusion