Blogger Information
Blog 14
fans 0
comment 0
visits 11547
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php多维数组创建及遍历
暮光薄凉
Original
1471 people have browsed it
1.常量声明

常量声明:define(“常量名”,”常量值”);
不能重新赋值,不需要$声明
define(“name”,”张三”);
echo name;

2.打印数组

var_dump : 打印数组的类型
print_r : 打印数组的值

3.数组创建

$arr = array();
$arry = [];
1.一维、二维、三维数组创建

  1. $xeu = [
  2. [
  3. 'id' => 1,
  4. 'name' => '张三',
  5. 'sex' => '男',
  6. 'aihao' =>['唱歌','打游戏'],
  7. ],
  8. ];
4.循环输出数组 :

foreach(数组名 as $key => $value){}
$key 数组的键,非必需值。$value 数组的值,名字可随便起
foreach 里面的key和value变量,被赋值后会保留最后一次循环的值
unset() :删除变量

  1. $a = [
  2. ['id' => 1,
  3. 'name' => '张三',
  4. 'sex' => '男',
  5. 'aihao' =>['唱歌','打游戏']
  6. ],
  7. ['id' => 2,
  8. 'name' => '李四',
  9. 'sex' => '男',
  10. 'aihao' =>['听歌','睡觉']
  11. ],
  12. ];
  13. // 遍历多维数组
  14. foreach($a as $key => $vale){
  15. echo $key;
  16. print_r( $vale);
  17. echo '<hr>';
  18. echo 'id:'.$vale['id'].',姓名:'.$vale['name'].',性别:'.$vale['sex'].',爱好:';
  19. foreach($vale['aihao'] as $k2 => $v2){
  20. // echo '--';
  21. // echo $k2;
  22. echo $v2;
  23. echo ',';
  24. }
  25. echo '<hr>';
  26. };

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:1.常量一般使用大写,与普通变量进行区分 2.普通博文不用提交
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post