array

(PHP 4, PHP 5, PHP 7, PHP 8)

arrayCreate an array

Description

array(mixed ...$values): array

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

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

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

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.

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!

?>

Notes

Note:

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

See Also

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

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Definition of stored procedures - an introduction

Stored procedures are predefined SQL code blocks that are stored in a database and can be called up as required. ...

Bernie

Autor : ebiz-consult GmbH & Co. KG
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial