数组 函数
PHP Manual

array

(PHP 4, PHP 5)

array 新建一个数组

说明

array array ([ mixed $... ] )

返回根据参数建立的数组。参数可以用 => 运算符给出索引。关于数组是什么的信息请阅读数组一节。

Note:

array() 是一个语言结构,用于字面上表示数组,不是常规的函数。

语法“index => values”,用逗号分开,定义了索引和值。索引可以是字符串或数字。如果省略了索引,会自动产生从 0 开始的整数索引。如果索引是整数,则下一个产生的索引将是目前最大的整数索引 + 1。注意如果定义了两个完全一样的索引,则后面一个会覆盖前一个。

在最后一个定义的数组项目之后加一个逗号虽然不常见,却是合法的语法。

下面的例子演示了怎样建立一个二维数组,怎样给相应的数组指定键名,以及怎样在普通数组中略过和继续数字索引。

Example #1 array() 例子

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

Example #2 array() 的自动索引

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

以上例程会输出:

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

注意索引 3 被定义了两次,保留了最后的值 13。索引 4 在 索引 8 之后定义,下一个自动生成的索引(值为 19 那个)为 9,因为最大的索引是 8。

本例建立了从 1 开始的数组。

Example #3 从 1 开始索引的 array()

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

以上例程会输出:

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

在 Perl 中,可以访问在双引号内的数组的值。但在 PHP 中需要将数组用花括号括起来。

Example #4 访问双引号内的数组

<?php

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

?>

参见 array_pad()list()count()foreachrange()

参数

...

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.

返回值

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.

范例

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 #5 array() example

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

Example #6 Automatic index with array()

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

以上例程会输出:

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 #7 1-based index with array()

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

以上例程会输出:

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 #8 Accessing an array inside double quotes

<?php

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

?>

注释

Note:

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

参见


数组 函数
PHP Manual