略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: Array 数组

2024-03-29

Array 数组

PHP 中的 array 实际上是一个有序映射。映射是一种把 values 关联到 keys 的类型。此类型针对多种不同用途进行了优化; 它可以被视为数组、列表(向量)、哈希表(映射的实现)、字典、集合、堆栈、队列等等。 由于 array 的值可以是其它 array 所以树形结构和多维 array 也是允许的。

解释这些数据结构超出了本手册的范围,但对每种结构至少会提供一个例子。 要得到这些数据结构的更多信息,建议参考有关此广阔主题的其它文献。

语法

定义数组 array()

可以用 array() 语言结构来新建一个 array。它接受任意数量用逗号分隔的 键(key) => 值(value) 对。

array(
    key  => value,
    key2 => value2,
    key3 => value3,
    ...
)

最后一个数组单元之后的逗号可以省略。通常用于单行数组定义中,例如常用 array(1, 2) 而不是 array(1, 2, )。对多行数组定义通常保留最后一个逗号,这样要添加一个新单元时更方便。

注意:

可以用短数组语法 [] 替代 array()

示例 #1 一个简单数组

<?php
$array 
= array(
    
"foo" => "bar",
    
"bar" => "foo",
);

// 使用短数组语法
$array = [
    
"foo" => "bar",
    
"bar" => "foo",
];
?>

key 可以是 integer 或者 stringvalue 可以是任意类型。

此外 key 会有如下的强制转换:

  • String 中包含有效的十进制 int,除非数字前面有一个 + 号,否则将被转换为 int 类型。例如键名 "8" 实际会被储存为 8。另外, "08" 不会被强制转换,因为它不是一个有效的十进制整数。
  • Float 也会被转换为 int ,意味着其小数部分会被舍去。例如键名 8.7 实际会被储存为 8
  • Bool 也会被转换成 int。即键名 true 实际会被储存为 1 而键名 false 会被储存为 0
  • Null 会被转换为空字符串,即键名 null 实际会被储存为 ""
  • Arrayobject 不能 被用为键名。坚持这么做会导致警告:Illegal offset type

如果在数组定义时多个元素都使用相同键名,那么只有最后一个会被使用,其它的元素都会被覆盖。

示例 #2 类型转换与覆盖的示例

<?php
$array 
= array(
    
1    => "a",
    
"1"  => "b",
    
1.5  => "c",
    
true => "d",
);
var_dump($array);
?>

以上例程会输出:

array(1) {
  [1]=>
  string(1) "d"
}

上例中所有的键名都被强制转换为 1,则每一个新单元都会覆盖前一个的值,最后剩下的只有一个 "d"

PHP 数组可以同时含有 intstring 类型的键名,因为 PHP 实际并不区分索引数组和关联数组。

示例 #3 混合 intstring 键名

<?php
$array 
= array(
    
"foo" => "bar",
    
"bar" => "foo",
    
100   => -100,
    -
100  => 100,
);
var_dump($array);
?>

以上例程会输出:

array(4) {
  ["foo"]=>
  string(3) "bar"
  ["bar"]=>
  string(3) "foo"
  [100]=>
  int(-100)
  [-100]=>
  int(100)
}

key 为可选项。如果未指定,PHP 将自动使用之前用过的最大 int 键名加上 1 作为新的键名。

示例 #4 没有键名的索引数组

<?php
$array 
= array("foo""bar""hello""world");
var_dump($array);
?>

以上例程会输出:

array(4) {
  [0]=>
  string(3) "foo"
  [1]=>
  string(3) "bar"
  [2]=>
  string(5) "hello"
  [3]=>
  string(5) "world"
}

还可以只对某些单元指定键名而对其它的空置:

示例 #5 仅对部分单元指定键名

<?php
$array 
= array(
         
"a",
         
"b",
    
=> "c",
         
"d",
);
var_dump($array);
?>

以上例程会输出:

array(4) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [6]=>
  string(1) "c"
  [7]=>
  string(1) "d"
}

可以看到最后一个值 "d" 被自动赋予了键名 7。这是由于之前最大的整数键名是 6

示例 #6 复杂类型转换和覆盖的例子

这个例子包括键名类型转换和元素覆盖的所有变化。

<?php
$array 
= array(
    
1    => 'a',
    
'1'  => 'b'// 值 "a" 会被 "b" 覆盖
    
1.5  => 'c'// 值 "b" 会被 "c" 覆盖
    
-=> 'd',
    
'01'  => 'e'// 由于这不是整数字符串,因此不会覆盖键名 1
    
'1.5' => 'f'// 由于这不是整数字符串,因此不会覆盖键名 1
    
true => 'g'// 值 "c" 会被 "g" 覆盖
    
false => 'h',
    
'' => 'i',
    
null => 'j'// 值 "i" 会被 "j" 覆盖
    
'k'// 值 “k” 的键名被分配为 2。这是因为之前最大的整数键是 1
    
=> 'l'// 值 "k" 会被 "l" 覆盖
);

var_dump($array);
?>

以上例程会输出:

array(7) {
  [1]=>
  string(1) "g"
  [-1]=>
  string(1) "d"
  ["01"]=>
  string(1) "e"
  ["1.5"]=>
  string(1) "f"
  [0]=>
  string(1) "h"
  [""]=>
  string(1) "j"
  [2]=>
  string(1) "l"
}

用方括号语法访问数组单元

数组单元可以通过 array[key] 语法来访问。

示例 #7 访问数组单元

<?php
$array 
= array(
    
"foo" => "bar",
    
42    => 24,
    
"multi" => array(
         
"dimensional" => array(
             
"array" => "foo"
         
)
    )
);

var_dump($array["foo"]);
var_dump($array[42]);
var_dump($array["multi"]["dimensional"]["array"]);
?>

以上例程会输出:

string(3) "bar"
int(24)
string(3) "foo"

注意:

在 PHP 8.0.0 之前,方括号和花括号可以互换使用来访问数组单元(例如 $array[42]$array{42} 在上例中效果相同)。 花括号语法在 PHP 7.4.0 中已弃用,在 PHP 8.0.0 中不再支持。

示例 #8 数组解引用

<?php
function getArray() {
    return array(
123);
}

$secondElement getArray()[1];

// 或
list(, $secondElement) = getArray();
?>

注意:

试图访问一个未定义的数组键名与访问任何未定义变量一样:会导致 E_NOTICE 级别错误信息,其结果为 null

注意:

数组解引用非 string 的标量值会产生 null。 在 PHP 7.4.0 之前,它不会发出错误消息。 从 PHP 7.4.0 开始,这个问题产生 E_NOTICE ; 从 PHP 8.0.0 开始,这个问题产生 E_WARNING

用方括号的语法新建/修改

可以通过明示地设定其中的值来修改现有的 array

这是通过在方括号内指定键名来给 array 赋值实现的。也可以省略键名,在这种情况下给变量名加上一对空的方括号([])。

$arr[key] = value;
$arr[] = value;
// key 可以是 intstring
// value 可以是任意类型的值

如果 $arr 不存在或者设置为 null 或者 false,将会新建它,这也是另一种创建 array 的方法。不过并不鼓励这样做,因为如果 $arr 已经包含有值(例如来自请求变量的 string)则此值会保留而 [] 实际上代表着字符串访问运算符。初始化变量的最好方式是直接给其赋值。

注意: 从 PHP 7.1.0 起,对字符串应用空索引操作符会抛出致命错误。以前,字符串被静默地转换为数组。

注意: 从 PHP 8.1.0 起,弃用从 false 值中创建一个新数组。 但仍然允许从 null 或者未定义的变量中创建新数组。

要修改某个值,通过其键名给该单元赋一个新值。要删除某键值对,对其调用 unset() 函数。

<?php
$arr 
= array(=> 112 => 2);

$arr[] = 56;    // 这与 $arr[13] = 56 相同;
                // 在脚本的这一点上

$arr["x"] = 42// 添加一个新元素
                // 键名使用 "x"
                
unset($arr[5]); // 从数组中删除元素

unset($arr);    // 删除整个数组
?>

注意:

如上所述,如果给出方括号但没有指定键名,则取当前最大 int 索引值,新的键名将是该值加上 1(但是最小为 0)。如果当前还没有 int 索引,则键名将为 0

注意这里所使用的最大整数键名目前不需要存在于 array。 它只要在上次 array 重新生成索引后曾经存在于 array 就行了。以下面的例子来说明:

<?php
// 创建一个简单的数组
$array = array(12345);
print_r($array);

// 现在删除其中的所有元素,但保持数组本身不变:
foreach ($array as $i => $value) {
    unset(
$array[$i]);
}
print_r($array);

// 添加一个单元(注意新的键名是 5,而不是你可能以为的 0)
$array[] = 6;
print_r($array);

// 重新索引:
$array array_values($array);
$array[] = 7;
print_r($array);
?>

以上例程会输出:

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

实用函数

有很多操作数组的函数,参见 数组函数 一节。

注意:

unset() 函数允许删除 array 中的某个键。但要注意数组将不会重建索引。如果需要删除后重建索引,可以用 array_values() 函数重建 array 索引。

<?php
$a 
= array(=> 'one'=> 'two'=> 'three');
unset(
$a[2]);
/* 该数组将被定义为
   $a = array(1 => 'one', 3 => 'three');
   而不是
   $a = array(1 => 'one', 2 =>'three');
*/

$b array_values($a);
// 现在 $b 是 array(0 => 'one', 1 =>'three')
?>

foreach 控制结构是专门用于 array的。它提供了一个简单的方法来遍历 array

数组做什么和不做什么

为什么 $foo[bar] 错了?

应该始终在用字符串表示的数组索引上加上引号。例如用 $foo['bar'] 而不是 $foo[bar]。但是为什么呢?可能在老的脚本中见过如下语法:

<?php
$foo
[bar] = 'enemy';
echo 
$foo[bar];
// 及其它
?>

这样是错的,但可以正常运行。那么为什么错了呢?原因是此代码中有一个未定义的常量( bar )而不是 string'bar'-注意引号)。而 PHP 可能会在以后定义此常量,不幸的是你的代码中有同样的名字。它能运行,是因为 PHP 自动将裸字符串(没有引号的 string 且不对应于任何已知符号)转换成一个其值为该裸 stringstring。例如,如果没有常量定义为 bar,那么PHP 将在 string 中替代为 'bar' 并使用之。

警告

将未定义的常量当作裸字符串的回退会触发 E_NOTICE 级别错误。 从 PHP 7.2.0 起已废弃,并触发 E_WARNING 级别错误。 从 PHP 8.0.0 起被移除,并触发 Error 异常。

注意: 这并不意味着总是给键名加上引号。用不着给键名为 常量变量 的加上引号,否则会使 PHP 不能解析它们。

<?php
error_reporting
(E_ALL);
ini_set('display_errors'true);
ini_set('html_errors'false);
// 简单数组:
$array = array(12);
$count count($array);
for (
$i 0$i $count$i++) {
    echo 
"\n检查 $i: \n";
    echo 
"坏的: " $array['$i'] . "\n";
    echo 
"好的: " $array[$i] . "\n";
    echo 
"坏的: {$array['$i']}\n";
    echo 
"好的: {$array[$i]}\n";
}
?>

以上例程会输出:

检查 0:
Notice: Undefined index:  $i in /path/to/script.html on line 9
坏的:
好的: 1
Notice: Undefined index:  $i in /path/to/script.html on line 11
坏的:
好的: 1

检查 1:
Notice: Undefined index:  $i in /path/to/script.html on line 9
坏的:
好的: 2
Notice: Undefined index:  $i in /path/to/script.html on line 11
坏的:
好的: 2

演示此行为的更多例子:

<?php
// 显示所有错误
error_reporting(E_ALL);

$arr = array('fruit' => 'apple''veggie' => 'carrot');

// 正确的
print $arr['fruit'];  // apple
print $arr['veggie']; // carrot

// 不正确的。  这可以工作,但也会抛出一个 E_NOTICE 级别的 PHP 错误,因为
// 未定义名为 apple 的常量
// 
// Notice: Use of undefined constant fruit - assumed 'fruit' in...
print $arr[fruit];    // apple

// 这定义了一个常量来演示正在发生的事情。  值 'veggie'
// 被分配给一个名为 fruit 的常量。
define('fruit''veggie');

// 注意这里的区别
print $arr['fruit'];  // apple
print $arr[fruit];    // carrot

// 以下是可以的,因为它在字符串中。
// 不会在字符串中查找常量,因此此处不会出现 E_NOTICE
print "Hello $arr[fruit]";      // Hello apple

// 有一个例外:字符串中花括号围绕的数组中常量可以被解释
//
print "Hello {$arr[fruit]}";    // Hello carrot
print "Hello {$arr['fruit']}";  // Hello apple

// 这将不起作用,并会导致解析错误,例如:
// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'
// 这当然也适用于在字符串中使用超全局变量
print "Hello $arr['fruit']";
print 
"Hello $_GET['foo']";

// 串联是另一种选择
print "Hello " $arr['fruit']; // Hello apple
?>

当打开 error_reporting 来显示 E_NOTICE 级别的错误(将其设为 E_ALL)时将看到这些错误。默认情况下 error_reporting 被关闭不显示这些。

和在 语法 一节中规定的一样,在方括号([” 和 “]”)之间必须有一个表达式。这意味着可以这样写:

<?php
echo $arr[somefunc($bar)];
?>

这是一个用函数返回值作为数组索引的例子。PHP 也可以用已知常量,可能之前已经见过:

<?php
$error_descriptions
[E_ERROR]   = "A fatal error has occurred";
$error_descriptions[E_WARNING] = "PHP issued a warning";
$error_descriptions[E_NOTICE]  = "This is just an informal notice";
?>

注意 E_ERROR 也是个合法的标识符,就和第一个例子中的 bar 一样。但是上一个例子实际上和如下写法是一样的:

<?php
$error_descriptions
[1] = "A fatal error has occurred";
$error_descriptions[2] = "PHP issued a warning";
$error_descriptions[8] = "This is just an informal notice";
?>

因为 E_ERROR 等于 1,等等。

那么为什么这样做不好?

也许有一天,PHP 开发小组可能会想新增一个常量或者关键字,或者用户可能希望以后在自己的程序中引入新的常量,那就有麻烦了。例如已经不能这样用 emptydefault 这两个词了,因为他们是 保留关键字

注意: 重申一次,在双引号字符串中,不给索引加上引号是合法的因此 "$foo[bar]" 是合法的(“合法”的原文为 valid。在实际测试中,这么做确实可以访问数组的该元素,但是会报一个常量未定义的 notice。无论如何,强烈建议不要使用 $foo[bar]这样的写法,而要使用 $foo['bar'] 来访问数组中元素。--haohappy 注)。至于为什么参见以上的例子和 字符串中的变量解析 中的解释。

转换为数组

对于任意 intfloatstringboolresource 类型,如果将一个值转换为 array,将得到一个仅有一个元素的数组,其下标为 0,该元素即为此标量的值。换句话说,(array)$scalarValuearray($scalarValue) 完全一样。

如果将 object 类型转换为 array,则结果为一个数组,其单元为该对象的属性。键名将为成员变量名,不过有几点例外:整数属性不可访问; 私有变量前会加上类名作前缀;保护变量前会加上一个 '*' 做前缀。这些前缀的前后都各有一个 NUL 字节。 未初始化的类型属性将会被丢弃。

<?php

class {
    private 
$B;
    protected 
$C;
    public 
$D;
    function 
__construct()
    {
        
$this->{1} = null;
    }
}

var_export((array) new A());
?>

以上例程会输出:

array (
  '' . "\0" . 'A' . "\0" . 'B' => NULL,
  '' . "\0" . '*' . "\0" . 'C' => NULL,
  'D' => NULL,
  1 => NULL,
)

这些 NUL 会导致一些意想不到的行为:

<?php

class {
    private 
$A// 将变为 '\0A\0A'
}

class 
extends {
    private 
$A// 将变为 '\0B\0A'
    
public $AA// 将变为 'AA'
}

var_dump((array) new B());
?>

以上例程会输出:

array(3) {
  ["BA"]=>
  NULL
  ["AA"]=>
  NULL
  ["AA"]=>
  NULL
}

上例会有两个键名为 'AA',不过其中一个实际上是 '\0A\0A'。

null 转换为 array 会得到一个空的数组。

比较

可以用 array_diff() 函数和 数组运算符 来比较数组。

数组解包

在 array 定义时,用 ... 前缀的一个 array 可以被展开到当前位置。 只有实现了 Traversable 的数组和对象才能被展开。 PHP 7.4.0 开始可以使用 ... 解包 array。

它可以多次使用,在 ... 操作符前后都可以添加常规元素:

示例 #9 简单的数组解包

<?php
// 使用简短的数组语法。
// 亦可用于 array() 语法
$arr1 = [123];
$arr2 = [...$arr1]; //[1, 2, 3]
$arr3 = [0, ...$arr1]; //[0, 1, 2, 3]
$arr4 = [...$arr1, ...$arr2111]; //[1, 2, 3, 1, 2, 3, 111]
$arr5 = [...$arr1, ...$arr1]; //[1, 2, 3, 1, 2, 3]

function getArr() {
  return [
'a''b'];
}
$arr6 = [...getArr(), 'c' => 'd']; //['a', 'b', 'c' => 'd']
?>

... 操作符解包 array 时也遵守函数 array_merge() 的语义。 也就是说,key 为字符时,后面的字符键会覆盖之前的字符键;key 为 integer 时则会重新编号:

示例 #10 重复 key 的数组解包

<?php
// string key
$arr1 = ["a" => 1];
$arr2 = ["a" => 2];
$arr3 = ["a" => 0, ...$arr1, ...$arr2];
var_dump($arr3); // ["a" => 2]

// integer key
$arr4 = [123];
$arr5 = [456];
$arr6 = [...$arr4, ...$arr5];
var_dump($arr6); // [1, 2, 3, 4, 5, 6]
// 即 [0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, 5 => 6]
// 也就是原始的 integer key 不再保留
?>

注意:

既不是 integer 也不是 string 的 key 会抛出 TypeError。 这类 key 只能通过 Traversable 对象生成。

注意:

在 PHP 8.1 之前,带有 string 键的 array 无法解包:

<?php

$arr1 
= [123];
$arr2 = ['a' => 4];
$arr3 = [...$arr1, ...$arr2];
// Fatal error: Uncaught Error: Cannot unpack array with string keys in example.php:5

$arr4 = [123];
$arr5 = [45];
$arr6 = [...$arr4, ...$arr5]; // works. [1, 2, 3, 4, 5]
?>

示例

PHP 中的数组类型有非常多的用途。以下是一些示例:

<?php
// This:
$a = array( 'color' => 'red',
            
'taste' => 'sweet',
            
'shape' => 'round',
            
'name'  => 'apple',
            
4        // 键名为 0
          
);

$b = array('a''b''c');

// . . .完全等同于:
$a = array();
$a['color'] = 'red';
$a['taste'] = 'sweet';
$a['shape'] = 'round';
$a['name']  = 'apple';
$a[]        = 4;        // 键名为 0

$b = array();
$b[] = 'a';
$b[] = 'b';
$b[] = 'c';

// 执行上述代码后,数组 $a 将是
// array('color' => 'red', 'taste' => 'sweet', 'shape' => 'round', 
// 'name' => 'apple', 0 => 4), 数组 $b 将是
// array(0 => 'a', 1 => 'b', 2 => 'c'), 或简单的 array('a', 'b', 'c').
?>

示例 #11 使用 array()

<?php
// Array as (property-)map
$map = array( 'version'    => 4,
              
'OS'         => 'Linux',
              
'lang'       => 'english',
              
'short_tags' => true
            
);
            
// 严格的数字键
$array = array( 7,
                
8,
                
0,
                
156,
                -
10
              
);
// 这相当于 array(0 => 7, 1 => 8, ...)

$switching = array(         10// key = 0
                    
5    =>  6,
                    
3    =>  7
                    
'a'  =>  4,
                            
11// key = 6 (整数索引的最大值为 5)
                    
'8'  =>  2// key = 8 (整数!)
                    
'02' => 77// key = '02'
                    
0    => 12  // 值 10 被 12 覆盖
                  
);
                  
// 空数组
$empty = array();         
?>

示例 #12 集合

<?php
$colors 
= array('red''blue''green''yellow');

foreach (
$colors as $color) {
    echo 
"Do you like $color?\n";
}

?>

以上例程会输出:

Do you like red?
Do you like blue?
Do you like green?
Do you like yellow?

可以通过引用传递 array 的值来直接更改数组的值。

示例 #13 在循环中改变单元

<?php
foreach ($colors as &$color) {
    
$color strtoupper($color);
}
unset(
$color); /* 确保后面对
$color 的写入不会修改最后一个数组元素 */

print_r($colors);
?>

以上例程会输出:

Array
(
    [0] => RED
    [1] => BLUE
    [2] => GREEN
    [3] => YELLOW
)

本例生成一个下标从 1 开始的数组。

示例 #14 下标从 1 开始的数组

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

以上例程会输出:

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

示例 #15 填充数组

<?php
// 把指定目录中的所有项填充到数组
$handle opendir('.');
while (
false !== ($file readdir($handle))) {
    
$files[] = $file;
}
closedir($handle); 
?>

Array 是有序的。也可以使用不同的排序函数来改变顺序。更多信息参见 数组函数。可以用 count() 函数来统计出 array 中元素的个数。

示例 #16 数组排序

<?php
sort
($files);
print_r($files);
?>

因为 array 中的值可以为任意值,也可是另一个 array。这样可以产生递归或多维 array

示例 #17 递归和多维数组

<?php
$fruits 
= array ( "fruits"  => array ( "a" => "orange",
                                       
"b" => "banana",
                                       
"c" => "apple"
                                     
),
                  
"numbers" => array ( 1,
                                       
2,
                                       
3,
                                       
4,
                                       
5,
                                       
6
                                     
),
                  
"holes"   => array (      "first",
                                       
=> "second",
                                            
"third"
                                     
)
                );

// 处理上面数组中的值的一些例子
echo $fruits["holes"][5];    // 打印 "second"
echo $fruits["fruits"]["a"]; // 打印 "orange"
unset($fruits["holes"][0]);  // 删除 "first"

// 创建一个新的多维数组
$juices["apple"]["green"] = "good"
?>

Array 的赋值总是会涉及到值的拷贝。使用 引用运算符 通过引用来拷贝 array

<?php
$arr1 
= array(23);
$arr2 $arr1;
$arr2[] = 4// $arr2 已更改,
             // $arr1 仍然是 array(2, 3)
             
$arr3 = &$arr1;
$arr3[] = 4// 现在 $arr1 和 $arr3 是一样的
?>
add a noteadd a note

User Contributed Notes 20 notes

up
121
mlvljr
11 years ago
please note that when arrays are copied, the "reference status" of their members is preserved (http://www.php.net/manual/en/language.references.whatdo.php).
up
59
thomas tulinsky
6 years ago
I think your first, main example is needlessly confusing, very confusing to newbies:

$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

It should be removed.

For newbies:
An array index can be any string value, even a value that is also a value in the array.
The value of array["foo"] is "bar".
The value of array["bar"] is "foo"

The following expressions are both true:
$array["foo"] == "bar"
$array["bar"] == "foo"
up
35
liberchen at gmail dot com
4 years ago
Since PHP 7.1, the string will not be converted to array automatically.

Below codes will fail:

$a=array();
$a['a']='';
$a['a']['b']=''; 
//Warning: Illegal string offset 'b'
//Warning: Cannot assign an empty string to a string offset

You have to change to as below:

$a=array();

$a['a']=array(); // Declare it is an array first
$a['a']['b']='';
up
52
ken underscore yap atsign email dot com
14 years ago
"If you convert a NULL value to an array, you get an empty array."

This turns out to be a useful property. Say you have a search function that returns an array of values on success or NULL if nothing found.

<?php $values = search(...); ?>

Now you want to merge the array with another array. What do we do if $values is NULL? No problem:

<?php $combined = array_merge((array)$values, $other); ?>

Voila.
up
53
jeff splat codedread splot com
17 years ago
Beware that if you're using strings as indices in the $_POST array, that periods are transformed into underscores:

<html>
<body>
<?php
    printf
("POST: "); print_r($_POST); printf("<br/>");
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="hidden" name="Windows3.1" value="Sux">
    <input type="submit" value="Click" />
</form>
</body>
</html>

Once you click on the button, the page displays the following:

POST: Array ( [Windows3_1] => Sux )
up
27
Yesterday php&#39;er
5 years ago
--- quote ---
Note:
Both square brackets and curly braces can be used interchangeably for accessing array elements
--- quote end ---

At least for php 5.4 and 5.6; if function returns an array, the curly brackets does not work directly accessing function result, eg. WillReturnArray(){1} . This will give "syntax error, unexpected '{' in...".
Personally I use only square brackets, expect for accessing single char in string. Old habits...
up
35
chris at ocportal dot com
9 years ago
Note that array value buckets are reference-safe, even through serialization.

<?php
$x
='initial';
$test=array('A'=>&$x,'B'=>&$x);
$test=unserialize(serialize($test));
$test['A']='changed';
echo
$test['B']; // Outputs "changed"
?>

This can be useful in some cases, for example saving RAM within complex structures.
up
38
ia [AT] zoznam [DOT] sk
16 years ago
Regarding the previous comment, beware of the fact that reference to the last value of the array remains stored in $value after the foreach:

<?php
foreach ( $arr as $key => &$value )
{
   
$value = 1;
}

// without next line you can get bad results...
//unset( $value );

$value = 159;
?>

Now the last element of $arr has the value of '159'. If we remove the comment in the unset() line, everything works as expected ($arr has all values of '1').

Bad results can also appear in nested foreach loops (the same reason as above).

So either unset $value after each foreach or better use the longer form:

<?php
foreach ( $arr as $key => $value )
{
   
$arr[ $key ] = 1;
}
?>
up
11
anisgazig at gmail dot com
3 years ago
//array keys are always integer and string data type and array values are all data type
//type casting and overwriting(data type of array key)
//----------------------------------------------------
$arr = array(
1=>"a",//int(1)
"3"=>"b",//int(3)
"08"=>"c",//string(2)"08"
"80"=>"d",//int(80)
"0"=>"e",//int(0)
"Hellow"=>"f",//string(6)"Hellow"
"10Hellow"=>"h",//string(8)"10Hellow"
1.5=>"j",//int(1.5)
"1.5"=>"k",//string(3)"1.5"
0.0=>"l",//int(0)
false=>"m",//int(false)
true=>"n",//int(true)
"true"=>"o",//string(4)"true"
"false"=>"p",//string(5)"false"
null=>"q",//string(0)""
NULL=>"r",//string(0)"" note null and NULL are same
"NULL"=>"s",//string(4)"NULL" ,,,In last element of multiline array,comma is better to used.
);
//check the data type name of key
foreach ($arr as $key => $value) {
var_dump($key);
echo "<br>";
}

//NOte :array and object data type in keys are Illegal ofset.......
up
24
caifara aaaat im dooaat be
16 years ago
[Editor's note: You can achieve what you're looking for by referencing $single, rather than copying it by value in your foreach statement. See http://php.net/foreach for more details.]

Don't know if this is known or not, but it did eat some of my time and maybe it won't eat your time now...

I tried to add something to a multidimensional array, but that didn't work at first, look at the code below to see what I mean:

<?php

$a1
= array( "a" => 0, "b" => 1 );
$a2 = array( "aa" => 00, "bb" => 11 );

$together = array( $a1, $a2 );

foreach(
$together as $single ) {
   
$single[ "c" ] = 3 ;
}

print_r( $together );
/* nothing changed result is:
Array
(
    [0] => Array
        (
            [a] => 0
            [b] => 1
        )
    [1] => Array
        (
            [aa] => 0
            [bb] => 11
        )
) */

foreach( $together as $key => $value ) {
   
$together[$key]["c"] = 3 ;
}

print_r( $together );

/* now it works, this prints
Array
(
    [0] => Array
        (
            [a] => 0
            [b] => 1
            [c] => 3
        )
    [1] => Array
        (
            [aa] => 0
            [bb] => 11
            [c] => 3
        )
)
*/

?>
up
4
dylanfj700 at gmail dot com
2 years ago
// Before php 5.4
$array = array(1,2,3);

// since php 5.4 , short syntax
$array = [1,2,3];

// I recommend using the short syntax if you have php version >= 5.4
up
20
lars-phpcomments at ukmix dot net
17 years ago
Used to creating arrays like this in Perl?

@array = ("All", "A".."Z");

Looks like we need the range() function in PHP:

<?php
$array
= array_merge(array('All'), range('A', 'Z'));
?>

You don't need to array_merge if it's just one range:

<?php
$array
= range('A', 'Z');
?>
up
1
ivail89 at mail dot ru
1 year ago
Function unset can delete array's element by reference only when you specify source array. See example:
<?php
$array
= [1, 2, 3, 4, 5];
foreach (
$array as $k => &$v){
    if (
$k >= 3){
        unset(
$v);
    }
}
echo
count($array); // 5
?>
In this case unset delete only reference, however original array didn't change.

Or different example:
<?php
$arr
= [1, 2];
$a = &$arr[0];
unset(
$a);
count($arr); // 2
?>

So for deleting element from first example need use key and array.
<?php
// ...
 
unset($array[$k]);
// ...
?>
up
6
note dot php dot lorriman at spamgourmet dot org
8 years ago
There is another kind of array (php>=  5.3.0) produced by

$array = new SplFixedArray(5);

Standard arrays, as documented here, are marvellously flexible and, due to the underlying hashtable, extremely fast for certain kinds of lookup operation.

Supposing a large string-keyed array

$arr=['string1'=>$data1, 'string2'=>$data2 etc....]

when getting the keyed data with

$data=$arr['string1'];

php does *not* have to search through the array comparing each key string to the given key ('string1') one by one, which could take a long time with a large array. Instead the hashtable means that php takes the given key string and computes from it the memory location of the keyed data, and then instantly retrieves the data. Marvellous! And so quick. And no need to know anything about hashtables as it's all hidden away.

However, there is a lot of overhead in that. It uses lots of memory, as hashtables tend to (also nearly doubling on a 64bit server), and should be significantly slower for integer keyed arrays than old-fashioned (non-hashtable) integer-keyed arrays. For that see more on SplFixedArray :

http://uk3.php.net/SplFixedArray

Unlike a standard php (hashtabled) array, if you lookup by integer then the integer itself denotes the memory location of the data, no hashtable computation on the integer key needed. This is much quicker. It's also quicker to build the array compared to the complex operations needed for hashtables. And it uses a lot less memory as there is no hashtable data structure. This is really an optimisation decision, but in some cases of large integer keyed arrays it may significantly reduce server memory and increase performance (including the avoiding of expensive memory deallocation of hashtable arrays at the exiting of the script).
up
0
phplatino at gmail dot com
10 months ago
to know the depth (dimension) of a ARRAY, you can use this:

function Dim_Ar($A, $i){
    if(!is_array($A))return 0;
    $t[] = 1;
    foreach($A AS $e)if(is_array($e))$t[] = Dim_Ar($e, ++ $i) + 1;
    return max($t);
    }

and use with:

$Q = ARRAY(ARRAY(ARRAY()), ARRAY(ARRAY()));// here depth/dimension is three

echo Dim_Ar($Q, 0);
up
1
tissus
4 years ago
In array(key=>value) construct key is also an expression.
This works fine:
  $a = array(
    1     =>0,
    1+1   =>1,
    $k    =>2,
    $x.'4'=>3
  );
up
-1
Denise Ignatova
4 years ago
When creating arrays , if we have an element with the same value as another element from the same array, we would expect PHP instead of creating new zval container to increase the refcount and point the duplicate symbol to the same zval. This is true except for value type integer.
Example:

$arr = ['bebe' => 'Bob', 'age' => 23, 'too' => 23 ];
xdebug_debug_zval( 'arr' );

Output:
arr:

(refcount=2, is_ref=0)
array (size=3)
  'bebe' => (refcount=1, is_ref=0)string 'Bob' (length=3)
  'age' => (refcount=0, is_ref=0)int 23
  'too' => (refcount=0, is_ref=0)int 23

but :
$arr = ['bebe' => 'Bob', 'age' => 23, 'too' => '23' ];
xdebug_debug_zval( 'arr' );

will produce:
arr:

(refcount=2, is_ref=0)
array (size=3)
  'bebe' => (refcount=1, is_ref=0)string 'Bob' (length=3)
  'age' => (refcount=0, is_ref=0)int 23
  'too' => (refcount=1, is_ref=0)string '23' (length=2)
or :

$arr = ['bebe' => 'Bob', 'age' => [1,2], 'too' => [1,2] ];
xdebug_debug_zval( 'arr' );

Output:
arr:

(refcount=2, is_ref=0)
array (size=3)
  'bebe' => (refcount=1, is_ref=0)string 'Bob' (length=3)
  'age' => (refcount=2, is_ref=0)
    array (size=2)
      0 => (refcount=0, is_ref=0)int 1
      1 => (refcount=0, is_ref=0)int 2
  'too' => (refcount=2, is_ref=0)
    array (size=2)
      0 => (refcount=0, is_ref=0)int 1
      1 => (refcount=0, is_ref=0)int 2
up
-13
ivegner at yandex dot ru
8 years ago
Note that objects of classes extending ArrayObject SPL class are treated as arrays, and not as objects when converting to array.

<?php
class ArrayObjectExtended extends ArrayObject
{
    private
$private = 'private';
    public
$hello = 'world';
}

$object = new ArrayObjectExtended();
$array = (array) $object;

// This will not expose $private and $hello properties of $object,
// but return an empty array instead.
var_export($array);
?>
up
-16
Walter Tross
11 years ago
It is true that "array assignment always involves value copying", but the copy is a "lazy copy". This means that the data of the two variables occupy the same memory as long as no array element changes.

E.g., if you have to pass an array to a function that only needs to read it, there is no advantage at all in passing it by reference.
up
-9
Anonymous
2 years ago
Wrappers for (array), returns array with normalize keys (without prefix):
<?php
function to_array_recursive($value): array
{
        if (!
is_object($value)) {
                return (array)
$value;
        }
       
$class = get_class($value);
       
$arr = [];
       
foreact ((array)  $value as $key => $val) {
               
$key = str_replace(["\0*\0", "\0{$class}\0"], '', $key);
               
$arr[$key] = is_object($val) ? to_array_recursive($val) : $val;
        }
        return
$arr;
}

function
to_array($value): array
{
       
$arr = (array) $value;
        if (!
is_object($value)) {
                return
$arr;
        }
       
$class = get_class($value);
       
$keys = str_replace(["\0*\0", "\0{$class}\0"], '', array_keys($arr));
        return
array_combine($keys, $arr);
}
?>
Demo:
<?php
class Test
{
        protected
$var = 1;
        protected
$var2;
        private
$TestVar = 3;
       
    public function
__construct($isParent = true)
    {
        if (
$isParent) {
           
$this->var2 = new self(! $isParent);
        }
    }
}

$obj = new Test();
var_dump((array) $obj, to_array_recursive($obj));
?>

官方地址:https://www.php.net/manual/en/language.types.array.php

冷却塔厂家 广告
-- 广告
北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3