PHP的数组 比 其他语言的数组要强大很多,下面我们看一下 数组的基本概念 和 数组的定义方式:
<?php
/**
* 数组
* 直接赋值声明数组,如下:
* $arr = array("one"=>"111111","two"=>"222222");
* 其中,one、two是key(键名),111111、222222是value(值、也叫键值)
*/
echo "---------- 索引数组 : 下标是整数 ----------
";
$arr[0] = 1;
$arr[1] = 2;
$arr[2] = 3;
print_r($arr); // 打印结果:Array ( [0] => 1 [1] => 2 [2] => 3 )
echo "
";
echo "---------- 关联数组 : 下标是字符串 ----------
";
$arr1['one'] = 1;
$arr1['two'] = 2;
$arr1['three'] = 3;
print_r($arr1); // 打印结果:Array ( [one] => 1 [two] => 2 [three] => 3 )
echo "
";
echo "---------- 数组 Demo ----------
";
$arr2 = $arr + $arr1;
print_r($arr2);
echo "
";
echo "---------- 数组 Demo 2 ----------
";
$arr3[] = 1;
$arr3[] = 2;
$arr3[] = 3;
$arr3[] = 4;
print_r($arr3);
echo "
";
/**
*
标签 函数的结果格式化输出,即,按原型打印。阅读起来就比较方便。
* echo '
';
* print_r($arr);
* echo '
';*/
echo "---------- 数组 Demo 3 ----------
";
$arr4 = array();
for ($i=0; $i<10; $i++){
$arr4[] = $i * $i;
}
echo '
';
print_r($arr4);
echo '
';/*
打印结果:
Array
(
[0] => 0
[1] => 1
[2] => 4
[3] => 9
[4] => 16
[5] => 25
[6] => 36
[7] => 49
[8] => 64
[9] => 81
)
*/
echo "---------- 数组 Demo 4 ----------
";
$arr5 = array();
for ($i=0; $i<10; $i++){
//echo $i."
";
if ($i == 4){
$arr5['刘德华'] = "欧巴";
}
if ($i == 7){
$arr5[-100] = 6666;
}
$arr5[] = $i * $i;
}
echo '
';
print_r($arr5);
echo '
';/*
打印结果:
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 ----------
";
$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.php.cn)!
本文原创发布php中文网,转载请注明出处,感谢您的尊重!