English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
array()The function is used to create a PHP array. This function can be used to create indexed arrays or associative arrays. PHP arrays can be one-dimensional or multi-dimensional.
Syntax for creating a PHP indexed array:
$a = array(value1, value2, value3, ...)
Syntax for creating a PHP associative array:
$a = array(key1 => value1, key2 => value2...)
Serial number | Parameters and descriptions |
---|---|
1 | key (optional) It specifies a numeric or string type key. If not set, an integer key starting from 0 is generated |
2 | value (required) It specifies the value |
This function returns the parameter array.
This function was originally as a core PHP 4Part of .0.0 introduced.
The following example creates an empty PHP array:
<?php $abc = array(); print_r($abc); ?>Test and see‹/›
Output result
Array ()
The following example creates a PHP index array containing a few elements:
<?php $abc = array("A", "B", "C"); print_r($abc); ?>Test and see‹/›
Output result
Array ( [0] => A [1=> B [2=> C )
The following example creates a PHP associative array with numeric keys:
<?php $abc = array(1 => "One", 2 3 => "Three" print_r($abc); ?>Test and see‹/›
Output result
Array ( [1=> One [2=> Two [3=> Three )
The following example creates a PHP associative array with string keys:
<?php $abc = array("one" => "One", "two" => "Two", "three" => "Three"); print_r($abc); ?>Test and see‹/›
Output result
Array ( [one] => One [two] => Two [three] => Three )
The following example explains how to add more values to an existing PHP array:
<?php $abc = array(1 => "One", 2 3 => "Three" print_r($abc); /*Add two more values to the array above */ $abc[4]= "Four"; $abc[5=> "Five"; print_r($abc); ?>Test and see‹/›
Output result
Array ( [1=> One [2=> Two [3=> Three ) Array ( [1=> One [2=> Two [3=> Three [4=> Four [5=> Five )
The following example demonstrates how to create a two-dimensional array, how to specify keys for associative arrays, and how to skip and continue with numeric indices in regular arrays.
<?php $fruits = array ( "fruits" => array("a" => "orange", "b" => "banana", "c" => "apple"), "numbers" => array(1, 2, 3, 4, 5, 6, "holes" => array("first", 5 => "second", "third") ); print_r($fruits); ?>Test and see‹/›
Output result
Array ( [fruits] => Array ( [a] => orange [b] => banana [c] => apple ) [numbers] => Array ( [0] => 1 [1=> 2 [2=> 3 [3=> 4 [4=> 5 [5=> 6 ) [holes] => Array ( [0] => first [5=> second [6=> third ) )
To delete a single array element or the entire array, you can useunset()Function:
<?php $abc = array(1 => "One", 2 3 => "Three" print_r($abc); /* Now let's delete the element at index2element*/ unset($abc[2]); print_r($abc); /* Now let's delete the entire array */ unset($abc); print_r($abc); ?>Test and see‹/›
Output result
Array ( [1=> One [2=> Two [3=> Three ) Array ( [1=> One [3=> Three ) PHP Notice: Undefined variable: abc in main.php on line 13