19 of 410 menu

The Array Construct

The array construct creates an array from the passed elements. Elements can be of any type: strings, numbers, other arrays, etc. The array is created with a zero index unless explicit keys are specified.

Syntax

array(value1, value2, ..., valueN);

Example

Let's create a simple array of numbers:

<?php $res = array(1, 2, 3, 4, 5); print_r($res); ?>

Code execution result:

[1, 2, 3, 4, 5]

Example

Let's create an associative array with explicit keys:

<?php $res = array( 'first' => 'a', 'second' => 'b', 'third' => 'c' ); print_r($res); ?>

Code execution result:

['first' => 'a', 'second' => 'b', 'third' => 'c']

Example

Let's create an array with different data types:

<?php $res = array('text', 123, true, array(1, 2, 3)); print_r($res); ?>

Code execution result:

['text', 123, true, [1, 2, 3]]

See Also

  • the array_push function,
    which adds elements to the end of an array
  • the array_merge function,
    which merges arrays
byenru