English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

Usage and examples of PHP array() function

PHP Array Functions

Definition and usage

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

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...)

Parameter

Serial numberParameters 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

Return value

This function returns the parameter array.

PHP version

This function was originally as a core PHP 4Part of .0.0 introduced.

Online example

The following example creates an empty PHP array:

<?php
   $abc = array();
   print_r($abc);
?>
Test and see‹/›

Output result

Array ()

Online example

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
)

Online example

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
)

Online example

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
)

Online example

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
)

Online example

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
        )
)

Online example

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

PHP Array Functions