Rechercher une fonction PHP

array

(PHP 4, PHP 5, PHP 7)

arrayCreate an array

Description

array ([ mixed $... ] ) : array

Creates an array. Read the section on the array type for more information on what an array is.

PHP: array - Manual Home of Manuel PHP  Contents Haut

Parameters

...

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 index are defined, the last overwrite the first.

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

PHP: array - Manual Home of Manuel PHP  Contents Haut

Return Values

Returns an array of the parameters. The parameters can be given an index with the => operator. Read the section on the array type for more information on what an array is.

PHP: array - Manual Home of Manuel PHP  Contents Haut

Examples

The following example demonstrates how to create a two-dimensional array, how to specify keys for associative arrays, and how to skip-and-continue numeric indices in normal arrays.

Example #1 array() example

<?php
$fruits 
= array (
    
"fruits"  => array("a" => "orange""b" => "banana""c" => "apple"),
    
"numbers" => array(123456),
    
"holes"   => array("first"=> "second""third")
);
?>

Example #2 Automatic index with array()

<?php
$array 
= array(1111,  1=> 1,  => 119=> 13);
print_r($array);
?>

The above example will output:

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

Note that index '3' is defined twice, and keep its final value of 13. Index 4 is defined after index 8, and next generated index (value 19) is 9, since biggest index was 8.

This example creates a 1-based array.

Example #3 1-based index with array()

<?php
$firstquarter 
= array(=> 'January''February''March');
print_r($firstquarter);
?>

The above example will output:

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

As in Perl, you can access a value from the array inside double quotes. However, with PHP you'll need to enclose your array between curly braces.

Example #4 Accessing an array inside double quotes

<?php

$foo 
= array('bar' => 'baz');
echo 
"Hello {$foo['bar']}!"// Hello baz!

?>

PHP: array - Manual Home of Manuel PHP  Contents Haut

Notes

Note:

array() is a language construct used to represent literal arrays, and not a regular function.

PHP: array - Manual Home of Manuel PHP  Contents Haut

See Also

  • array_pad() - Pad array to the specified length with a value
  • list() - Assign variables as if they were an array
  • count() - Count all elements in an array, or something in an object
  • range() - Create an array containing a range of elements
  • foreach
  • The array type

Find a PHP function

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-function.array.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.

References

  1. View the html document Language of the document:fr Manuel PHP : http://php.net

These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.

Contents Haut