PHP中的Array
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.数组和对象不能被用为键名。
键名不可重复,若重复,则只有最后一个有效。
<?php
$array = array(
1 => "a",
"1" => "b",
1.5 => "c",
true => "d",
);
var_dump($array);
?>
以上例程会输出:
array(1) {
[1]=>
string(1) "d"
}
上例中所有的键名都被强制转换为 1,则每一个新单元都会覆盖前一个的值,最后剩下的只有一个 "d"。
PHP数组可以同时含有int和string类型的键名,因为PHP 实际并不区分索引数组和关联数组。
如果对给出的值没有指定键名,则取当前最大的整数索引值,而新的键名将是该值加一。如果指定的键名已经有了值,则该值会被覆盖。
Example #5 仅对部分单元指定键名
<?php
$array = array(
"a",
"b",
6 => "c",
"d",
);
var_dump($array);
?>
以上例程会输出:
array(4) {
[0]=> //默认从0开始
string(1) "a"
[1]=>
string(1) "b"
[6]=>
string(1) "c"
[7]=> //没指定键,则从上个索引值+1
string(1) "d"
}
数组单元可以通过 array[key] 语法来访问。
Example #6 访问数组单元
<?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"
Note:
方括号和花括号可以互换使用来访问数组单元(例如 $array[42] 和 $array{42} 在上例中效果相同)。
要修改某个值,通过其键名给该单元赋一个新值。要删除某键值对,对其调用 unset() 函数。
<?php
$arr = array(5 => 1, 12 => 2); $arr[] = 56; // This is the same as $arr[13] = 56;
// at this point of the script $arr["x"] = 42; // This adds a new element to
// the array with key "x" unset($arr[5]); // This removes the element from the array unset($arr); // This deletes the whole array
?>
Note: 如上所述,如果给出方括号但没有指定键名,则取当前最大整数索引值,新的键名将是该值加上 1(但是最小为 0)。如果当前还没有整数索引,则键名将为 0。 注意这里所使用的最大整数键名不一定当前就在数组中。它只要在上次数组重新生成索引后曾经存在过就行了。以下面的例子来说明:
<?php
// 创建一个简单的数组
$array = array(1, 2, 3, 4, 5);
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
)
转换为数组
对于任意 integer,float,string,boolean 和 resource 类型,如果将一个值转换为数组,将得到一个仅有一个元素的数组,其下标为 0,该元素即为此标量的值。换句话说,(array)$scalarValue 与 array($scalarValue) 完全一样。
如果一个 object 类型转换为 array,则结果为一个数组,其单元为该对象的属性。键名将为成员变量名,不过有几点例外:整数属性不可访问;私有变量前会加上类名作前缀;保护变量前会加上一个 '*' 做前缀。这些前缀的前后都各有一个 NULL 字符。这会导致一些不可预知的行为:
<?php class A {
private $A; // This will become '\0A\0A'
} class B extends A {
private $A; // This will become '\0B\0A'
public $AA; // This will become 'AA'
} var_dump((array) new B());
?>
上例会有两个键名为 'AA',不过其中一个实际上是 '\0A\0A'。
将 NULL 转换为 array 会得到一个空的数组。
多用途的数组类型Array,以下为示例:
Example #8 使用 array()
<?php
// Array as (property-)map
$map = array( 'version' => 4,
'OS' => 'Linux',
'lang' => 'english',
'short_tags' => true
); // strictly numerical keys
$array = array( 7,
8,
0,
156,
-10
);
// this is the same as array(0 => 7, 1 => 8, ...) $switching = array( 10, // key = 0
5 => 6,
3 => 7,
'a' => 4,
11, // key = 6 (maximum of integer-indices was 5)
'8' => 2, // key = 8 (integer!)
'02' => 77, // key = '02'
0 => 12 // the value 10 will be overwritten by 12
); // empty array
$empty = array();
?>
Example #9 集合
<?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?
Example #10 在循环中改变单元
<?php
// PHP 5
foreach ($colors as &$color) {
$color = strtoupper($color);
}
unset($color); /* ensure that following writes to
$color will not modify the last array element */ // Workaround for older versions
foreach ($colors as $key => $color) {
$colors[$key] = strtoupper($color);
} print_r($colors);
?>
以上例程会输出:
Array
(
[0] => RED
[1] => BLUE
[2] => GREEN
[3] => YELLOW
)
Example #14 递归和多维数组
<?php
$fruits = array ( "fruits" => array ( "a" => "orange",
"b" => "banana",
"c" => "apple"
),
"numbers" => array ( 1,
2,
3,
4,
5,
6
),
"holes" => array ( "first",
5 => "second",
"third"
)
); // Some examples to address values in the array above
echo $fruits["holes"][5]; // prints "second"
echo $fruits["fruits"]["a"]; // prints "orange"
unset($fruits["holes"][0]); // remove "first" // Create a new multi-dimensional array
$juices["apple"]["green"] = "good";
?> 数组(Array) 的赋值总是会涉及到值的拷贝。使用引用运算符通过引用来拷贝数组。
<?php
$arr1 = array(2, 3);
$arr2 = $arr1;
$arr2[] = 4; // $arr2 is changed,
// $arr1 is still array(2, 3) $arr3 = &$arr1;
$arr3[] = 4; // now $arr1 and $arr3 are the same
?>
PHP中的Array的更多相关文章
- 了解PHP中的Array数组和foreach
1. 了解数组 PHP 中的数组实际上是一个有序映射.映射是一种把 values 关联到 keys 的类型.详细的解释可参见:PHP.net中的Array数组 . 2.例子:一般的数组 这里,我 ...
- 转OSGchina中,array老大的名词解释
转OSGchina中,array老大的名词解释 转自:http://ydwcowboy.blog.163.com/blog/static/25849015200983518395/ osg:: Cle ...
- C#中数组Array、ArrayList、泛型List<T>的比较
在C#中数组Array,ArrayList,泛型List都能够存储一组对象,但是在开发中根本不知道用哪个性能最高,下面我们慢慢分析分析. 一.数组Array 数组是一个存储相同类型元素的固定大小的顺序 ...
- 详解Javascript中的Array对象
基础介绍 创建数组 和Object对象一样,创建Array也有2种方式:构造函数.字面量法. 构造函数创建 使用构造函数的方式可以通过new关键字来声明,如下所示: 12 var arr = new ...
- ExtJS学习-----------Ext.Array,ExtJS对javascript中的Array的扩展
关于ExtJS对javascript中的Array的扩展.能够參考其帮助文档,文档下载地址:http://download.csdn.net/detail/z1137730824/7748893 因为 ...
- [原译]在mongoose中对Array Schema进行增删改
原文地址: http://tech-blog.maddyzone.com/node/add-update-delete-object-array-schema-mongoosemongodb 本文为上 ...
- JavaScript中数组Array方法详解
ECMAScript 3在Array.prototype中定义了一些很有用的操作数组的函数,这意味着这些函数作为任何数组的方法都是可用的. 1.Array.join()方法 Array.join()方 ...
- Swift中 删除Array的元素对象
Swift中Array的删除对象 在Swift中数组Array没有removeObject的方法 1.找到下标 let model_index = selectedArray.index(where: ...
- java 实现往oracle存储过程中传递array数组类型的参数
注:本文来源于 < java 实现往oracle存储过程中传递array数组类型的参数 >最近项目中遇到通过往存储过程传递数组参数的问题, 浪费了N多个小时,终于有点头绪. 具体的代码 ...
- 观V8源码中的array.js,解析 Array.prototype.slice为什么能将类数组对象转为真正的数组?
在官方的解释中,如[mdn] The slice() method returns a shallow copy of a portion of an array into a new array o ...
随机推荐
- DEDE仿站经常用到的基本标签和变量
一.针对于DEDE后台基本设置里面的使用到的数据标签. 主标题:{dede:global.cfg_webname/} 主要用于<title></title>里面 网 站描述: ...
- MS Sql Server 中主从库的配置和使用介绍(转)
网站规模到了一定程度之后,该分的也分了,该优化的也做了优化,但是还是不能满足业务上对性能的要求:这时候我们可以考虑使用主从库. 主从库是两台服务器上的两个数据库,主库以最快的速度做增删改操作+最新数据 ...
- Java错误:很奇怪的错误。。。
刚刚调试java web中出现了一个很奇怪的现象,前端有一个页面通过ajax调用后台的servlet,当我把后台的servlet代码修改后(将返回值由a修改为b),前端页面仍然获取的是a.调试跟踪se ...
- nyoj 73 比大小
点击打开链接 比大小 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 给你两个很大的数,你能不能判断出他们两个数的大小呢? 比如123456789123456789要大于 ...
- POJ 1542 Atlantis(线段树 面积 并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 参考网址:http://blog.csdn.net/sunmenggmail/article/d ...
- 【转】windows和linux间共享互传文件
原文:http://blog.guorunmin.cn/2015/09/16/windows%E5%92%8Clinux%E9%97%B4%E5%85%B1%E4%BA%AB%E4%BA%92%E4% ...
- Js字符串与十六进制的相互转换
开发过程中,字符串与十六进.二进制之间的相互转换常常会用到,尤其是涉及到中文的加密时,就需要把中文转换为十六进制.下面说说具体的转换方法. 1.字符串转换为十六进制 主要使用 charCodeAt() ...
- html标签属性
clientWidth = width + paddingclientHeight = height + paddingoffsetWidth = width + padding + borderof ...
- Throttling ASP.NET Web API calls
http://blog.maartenballiauw.be/post/2013/05/28/Throttling-ASPNET-Web-API-calls.aspx https://github.c ...
- OC基础(15)
@property参数 @Property练习 @class 循环retian *:first-child { margin-top: 0 !important; } body > *:last ...