Array in PHP

Array Syntax:

array ([key=>] value,)

Key may be an integer or string, and value may be any value.

$arr1 = array ('lang'=>'php', 1=>true, 13);
echo $arr1['lang'] ; // print php
echo $arr1[1];       // 1
echo $arr1[2];       // 13

As you can see above string 'key' is quoted.

An array in PHP is an ordered map. It is a type that maps value to keys. PHP array is very optimized. You can use PHP array in many ways.

Each value of PHP array can have another PHP array, so you can easily simulate trees.

PHP array types:

PHP has only one array type, which can contain integer and string indices.

Notice this:

// 1
array(5=>'xml', 'ajax', 'js', 'x'=>'html');
// 2
array(5=>'xml', 6=>'ajax', 7=>'js', 'x'=>'html');
/* Both "1" and "2" are same array for php.*/

Creating and modifying Array-

$arr2 = array(); // empty array
$arr2 = array(11=>'One', 12=>'Two');
$arr2[] = 'three';  // same as $arr2[13] = 'three';
$arr2['str1'] = 'Four';
// modify array for key=13
$arr2[13] = 'Three';
// Unset an array element:
unset ($arr2['str1']);
// Unset an whole array and not only an element:

Next: PHP array - Common functions