Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

array

Description

The array of Array for PHP create an array.

Syntax

array(
    mixed ...$values
): array

Parameters

values

Syntax "index => values", separated by commas, define index and values. index may be of type string or integer. When index is omitted, an integer index is automatically generated, starting at 0. If index is an integer, next generated index will be the biggest integer index + 1. Note that when two identical indices are defined, the last overwrites the first.

Having a trailing comma after the last defined array entry, while unusual, is a valid syntax.

Return

Returns an array of the parameters. The parameters can be given an index with the => operator.

Examples

1 · empty

<?

$return = array();

print_r($return);

?>
Array
(
)

2 · one-dimensional

<?

$return = array(0, 1);

print_r($return);

?>
Array
(
    [0] => 0
    [1] => 1
)

3 · two-dimensional

<?

$return = array (
"fruits"  => array("a" => "orange", "b" => "banana", "c" => "apple"),
"numbers" => array(1, 2, 3, 4, 5, 6),
"colors"  => array("red", 5 => "green", "blue")
);

print_r($return);

?>
Array
(
    [fruits] => Array
        (
            [a] => orange
            [b] => banana
            [c] => apple
        )

    [numbers] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
        )

    [colors] => Array
        (
            [0] => red
            [5] => green
            [6] => blue
        )

)

4 · automatic index

<?

$return = array(1, 1, 1, 1, 1, 8 => 1, 4 => 1, 19, 3 => 13);

print_r($return);

?>
Array
(
    [0] => 1
    [1] => 1
    [2] => 1
    [3] => 13
    [4] => 1
    [8] => 1
    [9] => 19
)

5 · 1-based index

<?

$return = array(1 => 'January', 'February', 'March');

print_r($return);

?>
Array
(
    [1] => January
    [2] => February
    [3] => March
)

6 · inside double quotes

<?

$return = array('name' => 'World');

echo "Hello {$return['name']}!";

?>
Hello World!
HomeMenu