PHP中的数组是一个有序映射(1对1的关系 key-value)。 Array是一个综合体:可表示数组、字典、集合等。 key可以是int或string。value可以是任意类型。 key如下情况会强制转换: 1.包含合法整型值的字符串=整型。 "8"=8 实际存储8 2.浮点数=整型。 8.7=8 小数
PHP中的数组是一个有序映射(1对1的关系 key->value)。
Array是一个综合体:可表示数组、字典、集合等。
key可以是int或string。value可以是任意类型。
key如下情况会强制转换:
1.包含合法整型值的字符串=>整型。 "8"=>8 实际存储8
2.浮点数=>整型。 8.7=>8 小数点会被舍去
3.布尔类型=>类型。 true=>1,false=>0 实际存储为0或1
4.Null=>“” 实际存储""
5.数组和对象不能被用为键名。
键名不可重复,若重复,则只有最后一个有效。
<span>php </span><span>$array</span> = <span>array</span><span>( </span>1 => "a", "1" => "b", 1.5 => "c", <span>true</span> => "d",<span> ); </span><span>var_dump</span>(<span>$array</span><span>); </span>?>
以上例程会输出:
<span>array</span>(1<span>) { [</span>1]=> <span>string</span>(1) "d"<span> }</span>
上例中所有的键名都被强制转换为 1,则每一个新单元都会覆盖前一个的值,最后剩下的只有一个 "d"。
PHP数组可以同时含有int和string类型的键名,因为PHP 实际并不区分索引数组和关联数组。
如果对给出的值没有指定键名,则取当前最大的整数索引值,而新的键名将是该值加一。如果指定的键名已经有了值,则该值会被覆盖。
Example #5 仅对部分单元指定键名
<span>php </span><span>$array</span> = <span>array</span><span>( </span>"a", "b", 6 => "c", "d",<span> ); </span><span>var_dump</span>(<span>$array</span><span>); </span>?>
以上例程会输出:
<span>array</span>(4<span>) { [</span>0]=> //默认从0开始 <span>string</span>(1) "a"<span> [</span>1]=> <span>string</span>(1) "b"<span> [</span>6]=> <span>string</span>(1) "c"<span> [</span>7]=> //没指定键,则从上个索引值+1 <span>string</span>(1) "d"<span> }</span>
数组单元可以通过 array[key] 语法来访问。
Example #6 访问数组单元
<span>php </span><span>$array</span> = <span>array</span><span>( </span>"foo" => "bar", 42 => 24, "multi" => <span>array</span><span>( </span>"dimensional" => <span>array</span><span>( </span>"array" => "foo"<span> ) ) ); </span><span>var_dump</span>(<span>$array</span>["foo"<span>]); </span><span>var_dump</span>(<span>$array</span>[42<span>]); </span><span>var_dump</span>(<span>$array</span>["multi"]["dimensional"]["array"<span>]); </span>?>
以上例程会输出:
<span>string</span>(3) "bar"<span> int(</span>24<span>) </span><span>string</span>(3) "foo"
Note:
方括号和花括号可以互换使用来访问数组单元(例如 $array[42] 和 $array{42} 在上例中效果相同)。
要修改某个值,通过其键名给该单元赋一个新值。要删除某键值对,对其调用 unset() 函数。
<span>php </span><span>$arr</span> = <span>array</span>(5 => 1, 12 => 2<span>); </span><span>$arr</span>[] = 56; <span>//</span><span> This is the same as $arr[13] = 56; // at this point of the script</span> <span>$arr</span>["x"] = 42; <span>//</span><span> This adds a new element to // the array with key "x"</span> <span>unset</span>(<span>$arr</span>[5]); <span>//</span><span> This removes the element from the array</span> <span>unset</span>(<span>$arr</span>); <span>//</span><span> This deletes the whole array</span> ?>
Note:<span> 如上所述,如果给出方括号但没有指定键名,则取当前最大整数索引值,新的键名将是该值加上 </span>1(但是最小为 0)。如果当前还没有整数索引,则键名将为 0<span>。 注意这里所使用的最大整数键名不一定当前就在数组中。它只要在上次数组重新生成索引后曾经存在过就行了。以下面的例子来说明:</span>
<span>php </span><span>//</span><span> 创建一个简单的数组</span> <span>$array</span> = <span>array</span>(1, 2, 3, 4, 5<span>); </span><span>print_r</span>(<span>$array</span><span>); </span><span>//</span><span> 现在删除其中的所有元素,但保持数组本身不变:</span> <span>foreach</span> (<span>$array</span> <span>as</span> <span>$i</span> => <span>$value</span><span>) { </span><span>unset</span>(<span>$array</span>[<span>$i</span><span>]); } </span><span>print_r</span>(<span>$array</span><span>); </span><span>//</span><span> 添加一个单元(注意新的键名是 5,而不是你可能以为的 0)</span> <span>$array</span>[] = 6<span>; </span><span>print_r</span>(<span>$array</span><span>); </span><span>//</span><span> 重新索引:</span> <span>$array</span> = <span>array_values</span>(<span>$array</span><span>); </span><span>$array</span>[] = 7<span>; </span><span>print_r</span>(<span>$array</span><span>); </span>?>
以上例程会输出:
<span>Array</span><span> ( [</span>0] => 1<span> [</span>1] => 2<span> [</span>2] => 3<span> [</span>3] => 4<span> [</span>4] => 5<span> ) </span><span>Array</span><span> ( ) </span><span>Array</span><span> ( [</span>5] => 6<span> ) </span><span>Array</span><span> ( [</span>0] => 6<span> [</span>1] => 7<span> )</span>
转换为数组
对于任意 integer,float,string,boolean 和 resource 类型,如果将一个值转换为数组,将得到一个仅有一个元素的数组,其下标为 0,该元素即为此标量的值。换句话说,(array)$scalarValue 与 array($scalarValue) 完全一样。
如果一个 object 类型转换为 array,则结果为一个数组,其单元为该对象的属性。键名将为成员变量名,不过有几点例外:整数属性不可访问;私有变量前会加上类名作前缀;保护变量前会加上一个 '*' 做前缀。这些前缀的前后都各有一个 NULL 字符。这会导致一些不可预知的行为:
<span>php </span><span>class</span><span> A { </span><span>private</span> <span>$A</span>; <span>//</span><span> This will become '\0A\0A'</span> <span>} </span><span>class</span> B <span>extends</span><span> A { </span><span>private</span> <span>$A</span>; <span>//</span><span> This will become '\0B\0A'</span> <span>public</span> <span>$AA</span>; <span>//</span><span> This will become 'AA'</span> <span>} </span><span>var_dump</span>((<span>array</span>) <span>new</span><span> B()); </span>?>
上例会有两个键名为 'AA',不过其中一个实际上是 '\0A\0A'。
将 NULL 转换为 array 会得到一个空的数组。
多用途的数组类型Array,以下为示例:
Example #8 使用 array()
<span>php </span><span>//</span><span> Array as (property-)map</span> <span>$map</span> = <span>array</span>( 'version' => 4, 'OS' => 'Linux', 'lang' => 'english', 'short_tags' => <span>true</span><span> ); </span><span>//</span><span> strictly numerical keys</span> <span>$array</span> = <span>array</span>( 7, 8, 0, 156, -10<span> ); </span><span>//</span><span> this is the same as array(0 => 7, 1 => 8, ...)</span> <span>$switching</span> = <span>array</span>( 10, <span>//</span><span> key = 0</span> 5 => 6, 3 => 7, 'a' => 4, 11, <span>//</span><span> key = 6 (maximum of integer-indices was 5)</span> '8' => 2, <span>//</span><span> key = 8 (integer!)</span> '02' => 77, <span>//</span><span> key = '02'</span> 0 => 12 <span>//</span><span> the value 10 will be overwritten by 12</span> <span> ); </span><span>//</span><span> empty array</span> <span>$empty</span> = <span>array</span><span>(); </span>?>
Example #9 集合
<span>php </span><span>$colors</span> = <span>array</span>('red', 'blue', 'green', 'yellow'<span>); </span><span>foreach</span> (<span>$colors</span> <span>as</span> <span>$color</span><span>) { </span><span>echo</span> "Do you like <span>$color</span>?\n"<span>; } </span>?>
以上例程会输出:
<span>Do you like red? Do you like blue? Do you like green? Do you like yellow?</span>
Example #10 在循环中改变单元
<span>php </span><span>//</span><span> PHP 5</span> <span>foreach</span> (<span>$colors</span> <span>as</span> &<span>$color</span><span>) { </span><span>$color</span> = <span>strtoupper</span>(<span>$color</span><span>); } </span><span>unset</span>(<span>$color</span>); <span>/*</span><span> ensure that following writes to $color will not modify the last array element </span><span>*/</span> <span>//</span><span> Workaround for older versions</span> <span>foreach</span> (<span>$colors</span> <span>as</span> <span>$key</span> => <span>$color</span><span>) { </span><span>$colors</span>[<span>$key</span>] = <span>strtoupper</span>(<span>$color</span><span>); } </span><span>print_r</span>(<span>$colors</span><span>); </span>?>
以上例程会输出:
<span>Array ( [0] => RED [1] => BLUE [2] => GREEN [3] => YELLOW )</span>
Example #14 递归和多维数组
<span>php </span><span>$fruits</span> = <span>array</span> ( "fruits" => <span>array</span> ( "a" => "orange", "b" => "banana", "c" => "apple"<span> )</span>, "numbers" => <span>array</span> ( 1, 2, 3, 4, 5, 6<span> )</span>, "holes" => <span>array</span> ( "first", 5 => "second", "third"<span> ) ); </span><span>//</span><span> Some examples to address values in the array above </span> <span>echo</span> <span>$fruits</span>["holes"][5]; <span>//</span><span> prints "second"</span> <span>echo</span> <span>$fruits</span>["fruits"]["a"]; <span>//</span><span> prints "orange"</span> <span>unset</span>(<span>$fruits</span>["holes"][0]); <span>//</span><span> remove "first" // Create a new multi-dimensional array</span> <span>$juices</span>["apple"]["green"] = "good"<span>; </span>?><span> 数组(</span><span>Array</span><span>) 的赋值总是会涉及到值的拷贝。使用引用运算符通过引用来拷贝数组。 </span><span>php </span><span>$arr1</span> = <span>array</span>(2, 3<span>); </span><span>$arr2</span> = <span>$arr1</span><span>; </span><span>$arr2</span>[] = 4; <span>//</span><span> $arr2 is changed, // $arr1 is still array(2, 3)</span> <span>$arr3</span> = &<span>$arr1</span><span>; </span><span>$arr3</span>[] = 4; <span>//</span><span> now $arr1 and $arr3 are the same</span> ?>