PHP中对数组的操作
数组的声明 PHP中有两种数组:索引数组和关联数组。索引数组的索引是从0开始递增的数字,由程序自动生成;关联数组使用字符串作为索引值,由用户自行输入。 初始化时直接赋值 1 2 3 4 5 6 7 8 9 //索引数组 $example[0] = "a"; $example[1] = "b"; $example[2] = "c"; //关联数组 $example_1["a"] = "1"; $example_1["b"] = "2"; $example_1["c"] = "3"; 若要按默认索引顺序声明索引数组,可以不用填入索引值,程序自动按声明顺序为键值建立索引: 1 2 3 $example[] = "a"; //索引为0 $example[] = "b"; //索引为1 $example[] = "c"; //索引为2 通过array()函数创建 1 2 3 4 5 //索引数组 $example = array("a", "b", "c"); //关联数组 $example_1 = array("a" => "1", "b" => "2", "c" => "3"); 多维数组的创建使用array()函数嵌套完成: 1 2 3 4 5 $example = array( "array1" => array("a", "b", "c"), "array2" => array("d", "e", "f"), "array3" => array("g", "h", "i") ) 数组的遍历 使用for循环遍历 1 2 3 4 5 6 7 8 9 <?php $example = array("a", "b", "c"); for($i = 0; $i < count($example); $i++){ echo $example[$i]."<br>"; } ?> 打印结果: ...