时间:2021-07-01 10:21:17 帮助过:12人阅读
<?php /** * 数组 * 直接赋值声明数组,如下: * $arr = array("one"=>"111111","two"=>"222222"); * 其中,one、two是key(键名),111111、222222是value(值、也叫键值) */ echo "---------- 索引数组 : 下标是整数 ----------<br>"; $arr[0] = 1; $arr[1] = 2; $arr[2] = 3; print_r($arr); // 打印结果:Array ( [0] => 1 [1] => 2 [2] => 3 ) echo "<br>"; echo "---------- 关联数组 : 下标是字符串 ----------<br>"; $arr1['one'] = 1; $arr1['two'] = 2; $arr1['three'] = 3; print_r($arr1); // 打印结果:Array ( [one] => 1 [two] => 2 [three] => 3 ) echo "<br>"; echo "---------- 数组 Demo ----------<br>"; $arr2 = $arr + $arr1; print_r($arr2); echo "<br>"; echo "---------- 数组 Demo 2 ----------<br>"; $arr3[] = 1; $arr3[] = 2; $arr3[] = 3; $arr3[] = 4; print_r($arr3); echo "<br>"; /** * <pre>标签 函数的结果格式化输出,即,按原型打印。阅读起来就比较方便。 * echo '<pre>'; * print_r($arr); * echo '</pre>'; */ echo "---------- 数组 Demo 3 ----------<br>"; $arr4 = array(); for ($i=0; $i<10; $i++){ $arr4[] = $i * $i; } echo '<pre>'; print_r($arr4); echo '</pre>'; /* 打印结果: Array ( [0] => 0 [1] => 1 [2] => 4 [3] => 9 [4] => 16 [5] => 25 [6] => 36 [7] => 49 [8] => 64 [9] => 81 ) */ echo "---------- 数组 Demo 4 ----------<br>"; $arr5 = array(); for ($i=0; $i<10; $i++){ //echo $i."<br>"; if ($i == 4){ $arr5['刘德华'] = "欧巴"; } if ($i == 7){ $arr5[-100] = 6666; } $arr5[] = $i * $i; } echo '<pre>'; print_r($arr5); echo '</pre>'; /* 打印结果: Array ( [0] => 0 [1] => 1 [2] => 4 [3] => 9 [刘德华] => 欧巴 [4] => 16 [5] => 25 [6] => 36 [-100] => 6666 [7] => 49 [8] => 64 [9] => 81 ) */ /** * 以下是几种定义数组的方式: */ echo "---------- 数组 Demo 5 ----------<br>"; $arr6 = array(1,2,3,4,5,6); $arr7 = array("one","two","three"); $arr8 = array(0=>"aaa",1=>"bbb",2=>"ccc"); $arr9 = array("aaa",6=>"bbb","ccc"); $arr10 = array("name"=>"wang","age"=>20);
以上就是Android程序员学PHP开发(19)-数组(1)基本概念和定义方式-PhpStorm的内容,更多相关内容请关注PHP中文网(www.gxlcms.com)!