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

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

Example

Let's create a simple array of numbers:

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

Code execution result:

php[1, 2, 3, 4, 5]

Example

Let's create an associative array with explicit keys:

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

Code execution result:

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

Example

Let's create an array with different data types:

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

Code execution result:

php['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