作为一名C++程序员,在转做PHP开发的过程中,对PHP数组产生了一些混淆,与C++数组有相似的地方,也有一些不同,下面就全面地分析一下PHP的数组及其与C++中相应数据类型的区别和联系。
1、数值数组:也叫索引数组,即以数字(从0 开始)作为数组下标。相当于C++中的vector。
2、关联数组:以字符串作为数组下标。相当于C++中的map。
3、多维数组:数组中每个元素也是一个数组。其子数组中的每个元素也可以是数组。
1、数值数组
<span>a、如下例子中,会自动分配数字ID键。 </span><span>$names</span> = <span>array</span>("Peter","Joe","Lily"<span>); b、如下例子中,我们人工分配数字ID键。 </span><span>$names</span>[0] = "Peter"<span>; </span><span>$names</span>[1] = "Joe"<span>; </span><span>$names</span>[2] = "Lily"<span>; 可以在脚本中使用这些ID键: </span><?<span>php </span><span>$names</span>[0] = "Peter"<span>; </span><span>$names</span>[1] = "Joe"<span>; </span><span>$names</span>[2] = "Lily"<span>; </span><span>echo</span> <span>$names</span>[0]." and ".<span>$names</span>[1]." are ".<span>$names</span>[2]."'s neighbors"<span>; </span><span>/*</span><span> 何问起 hovertree.com </span><span>*/</span> ?>
2、关联数组:
<span>例子1 </span><span>$ages</span> = <span>array</span>("Peter"=>32, "Joe"=>30, "Lily"=>28<span>); 例子2 本例与例子1相同,只是另一种创建数组的方法。 </span><span>$ages</span>["Peter"] = "32"<span>; </span><span>$ages</span>["Joe"] = "30"<span>; </span><span>$ages</span>["Lily"] = "28"<span>; 在脚本中使用关联数组: </span><?<span>php </span><span>$ages</span>["Peter"] = "32"<span>; </span><span>$ages</span>["Joe"] = "30"<span>; </span><span>$ages</span>["Lily"] = "28"<span>; </span><span>echo</span> "Peter is ".<span>$ages</span>["Peter"]." years old."<span>; </span><span>/*</span><span> 何问起 hovertree.com </span><span>*/</span> ?><span> 以上脚本输出: Peter is </span>32 years old.
3、多维数组:
<span>在本例中,我们创建了一个带有自动分配数字ID键的多维数组: </span><span>$families</span> = <span>array</span><span> { </span>"Griffin"=><span>array</span><span> { </span>"Peter", "Lois", "Megan"<span> }</span>, "Quagmire"=><span>array</span><span> { </span>"Glenn"<span> }</span>, "Brown"=><span>array</span><span> { </span>"Cleveland", "Loretta", "Junior"<span> } }; </span><span>echo</span> "Is " . <span>$families</span>['Griffin'][2] . " a part of the Griffin family?"<span>; 以上代码输出: Is Megan a part of the Griffin family</span>?
1<span>、for循环遍历 for循环只能遍历索引数组。 </span><?<span>php </span><span>$names</span> = <span>array</span>("Peter","Joe","Lily"<span>); </span><span>for</span>(<span>$id</span>=0;<span>$id</span><<span>count</span>(<span>$names</span>);++<span>$id</span><span>) { </span><span>echo</span> <span>$names</span>[<span>$id</span><span>]; } </span>?> 2<span>、foreach遍历 即可以遍历索引数组,也可以遍历关联数组 遍历索引数组 </span><span>foreach</span>(array_expression <span>as</span> <span>$value</span><span>) { 循环体; } 遍历关联数组 </span><span>foreach</span>(array_expression <span>as</span> <span>$key</span>=><span>$value</span><span>) { 循环体; } A、一维数组遍历 索引数组 </span><?<span>php </span><span>$contact</span> = <span>array</span>("李某","xx公司","abc@xx.com"<span>); </span><span>foreach</span>(<span>$contact</span> <span>as</span> <span>$value</span><span>) { </span><span>echo</span> <span>$value</span><span>; } </span>?> <?<span>php </span><span>$contact</span> = <span>array</span>("姓名"=>"李某","公司"=>"xx公司","邮箱"=>"abc@xx.com"<span>); </span><span>foreach</span>(<span>$contact</span> <span>as</span> <span>$key</span>=><span>$value</span><span>) { </span><span>echo</span> <span>$key</span>.":".<span>$value</span><span>; } </span>?><span> B、多维数组遍历 </span><?<span>php </span><span>$wage</span> = <span>array</span><span>( </span>"市场部"=<span>array</span><span>( </span><span>array</span>(1,"李某","市场经理",8000), <span>array</span>(2,"王某","市场专员",5000), <span>array</span>(3,"刘某","市场专员",7000<span>) )</span>, "产品部"=<span>array</span><span>( </span><span>array</span>(1,"李某","产品经理",9000), <span>array</span>(2,"王某","产品专员",6000), <span>array</span>(3,"刘某","产品专员",5000<span>) )</span>, "账务部"=<span>array</span><span>( </span><span>array</span>(1,"李某","账务经理",7000), <span>array</span>(2,"王某","账务专员",6000), <span>array</span>(3,"刘某","账务专员",5000<span>) ) ); </span><span>foreach</span>(<span>$wage</span> <span>as</span> <span>$section</span>=><span>$table</span><span>) { </span><span>echo</span> <span>$section</span>."部门人员如下"<span>; </span><span>foreach</span>(<span>$table</span> <span>as</span> <span>$row</span><span>) { </span><span>foreach</span>(<span>$row</span> <span>as</span> <span>$value</span><span>) { </span><span>echo</span> <span>$value</span><span>; } } } </span><span>/*</span><span> 何问起 hovertree.com </span><span>*/</span> ?>
推荐:http://www.cnblogs.com/roucheng/p/phpdongtai.html