PHP学习笔记 06 - 数组
2017-11-21 13:50
387 查看
PHP 数组
使用 array() 创建数组类型:索引数组、关联数组
索引数组
语法:array(element1, element2, …)访问元素:$arr[index]
count() 返回数组长度
示例:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <title>Indexed Array</title> </head> <body> <div class="well"> <?php $fav_langs = array('PHP', 'C++', 'Java', 'Python', 'Go'); echo 'My favorite programming languages are ' . $fav_langs[0]; $arrlength = count($fav_langs); for ($i = 1; $i < $arrlength - 1; ++$i) { echo ', ' . $fav_langs[$i]; } echo ' and ' . $fav_langs[$arrlength - 1] . '<br>'; ?> </div> </body> </html>
查看运行结果
关联数组
关联数组类似 Map,存放键值对语法:array(key1=>value1, key2=>value2, …)
访问元素:$arr[key]
使用 foreach 遍历关联数组:
foreach($arr as $key => $value) { // do something using $key and $value }
示例:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <title>Associative Array</title> </head> <body> <div class="well"> <?php $fav_colors = array('red'=>'#FF0000', 'green'=>'#00FF00', 'blue'=>'#0000FF'); foreach ($fav_colors as $color => $value) { echo '<span style="color:' . $value . '">' . $color . ': ' . $value . '</span><br>'; } ?> </div> </body> </html>
查看运行结果
打印数组
使用 print_r() 打印数组数组排序
函数 | 说明 |
---|---|
short() | 数组升序排序 |
rsort() | 数组降序排序 |
asort() | 关联数组按值升序排序 |
ksort() | 关联数组按键升序排序 |
arsort() | 关联数组按值降序排序 |
krsort() | 关联数组按键降序排序 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <title>Sorting Arrays</title> </head> <body> <div class="well"> <?php function print_indexed_arr($arr) { for ($i = 0; $i < count($arr); ++$i) { echo $arr[$i] . ' '; } } function print_associative_arr($arr) { foreach ($arr as $color => $value) { echo $color . '=>' . $value . ' '; } } echo '<h2>Sorting indexed array</h2>'; $fav_langs = array('PHP', 'C++', 'Java', 'Python', 'Go'); echo 'origin:<br>'; print_indexed_arr($fav_langs); echo '<br>'; sort($fav_langs); echo 'ascending order:<br>'; print_indexed_arr($fav_langs); echo '<br>'; rsort($fav_langs); echo 'descending order:<br>'; print_indexed_arr($fav_langs); echo '<br><br>'; echo '<h2>Sorting associative array</h2>'; $fav_colors = array('red'=>'#FF0000', 'chocolate'=>'#D2691E', 'green'=>'#00FF00', 'blue'=>'#0000FF'); echo 'origin:<br>'; print_associative_arr($fav_colors); echo '<br>'; asort($fav_colors); echo 'ascending order by value:<br>'; print_associative_arr($fav_colors); echo '<br>'; ksort($fav_colors); echo 'ascending order by key:<br>'; print_associative_arr($fav_colors); echo '<br>'; arsort($fav_colors); echo 'descending order by value:<br>'; print_associative_arr($fav_colors); echo '<br>'; krsort($fav_colors); echo 'descending order by key:<br>'; print_associative_arr($fav_colors); echo '<br>'; ?> </div> </body> </html>
查看运行结果
数组的增删改
修改元素的值
修改索引数组元素的值:$arr[index] = newValue;,index 从 0 开始
修改关联数组元素的值:
$arr[keyName] = newValue;
示例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <title>Array Modification</title> </head> <body> <div class="well"> <?php function print_indexed_arr($arr) { for ($i = 0; $i < count($arr); ++$i) { echo $arr[$i] . ' '; } } function print_associative_arr($arr) { foreach ($arr as $color => $value) { echo $color . '=>' . $value . ' '; } } // 修改索引数组中的元素 echo('<h2>Indexed Array Modification</h2>'); $fav_langs = array('PHP', 'C++', 'Java', 'Python', 'Go'); echo('origin:<br>'); print_indexed_arr($fav_langs); echo('<br>'); $fav_langs[1] = 'C/C++'; // 修改第2个元素的值 $fav_langs[4] = 'Golang'; // 修改第5个元素的值 echo('after modification:<br>'); print_indexed_arr($fav_langs); echo('<br>'); // 修改关联数组中的元素 echo('<h2>Associative Array Modification</h2>'); $fav_colors = array('red'=>'#FF0000', 'green'=>'#00FF00', 'blue'=>'#0000FF'); echo('origin:<br>'); print_associative_arr($fav_colors); echo('<br>'); $fav_colors['red'] = '#EE0000'; // 修改key为'red'的元素的值 $fav_colors['green'] = '#00EE00'; // 修改key为'green'的元素的值 $fav_colors['blue'] = '#0000EE'; // 修改key为'blue'的元素的值 echo('after modification:<br>'); print_associative_arr($fav_colors); echo('<br>'); ?> </div> </body> </html>
查看运行结果
增加元素
增加元素到索引数组
函数 | 说明 |
---|---|
array_push(array,value1,value2…) | 插入元素到数组末尾,不能应用于关联数组 |
array_unshift(array,value1,value2…) | 插入元素到数组开头,不能应用于关联数组 |
增加元素到关联数组
$arr['newKey'] = value
示例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <title>Inserting Element to Array</title> </head> <body> <div class="well"> <?php echo '<h2>Inserting Elements to Indexed Array</h2>'; $fav_langs = array('PHP', 'C++', 'Java', 'Python', 'Go'); echo 'origin:<br>'; print_r($fav_langs); echo '<br>'; echo 'Push element to back:<br>'; array_push($fav_langs, 'C#', 'VB'); // 追加元素到末尾 print_r($fav_langs); echo '<br>'; echo 'Insert element to front:<br>'; array_unshift($fav_langs, 'Perl', 'R'); // 插入元素到开头 print_r($fav_langs); echo '<br>'; echo '<h2>Inserting Elements to Associative Array</h2>'; $fav_colors = array('red'=>'#FF0000', 'green'=>'#00FF00', 'blue'=>'#0000FF'); echo 'origin:<br>'; print_r($fav_colors); echo '<br>'; $fav_colors['yellow'] = '#FFFF00'; // 插入元素到关联数组 echo 'After insert element:<br>'; print_r($fav_colors); echo '<br>'; ?> </div> </body> </html>
查看运行结果
删除元素
函数 | 说明 |
---|---|
array_pop(array) | 删除末尾元素,返回删除的元素 |
array_shift(array) | 删除开头元素,返回删除的元素 |
示例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <title>Removing Element from Array</title> </head> <body> <div class="well"> <?php echo '<h2>Removing Elements from Indexed Array</h2>'; $fav_langs = array('PHP', 'C++', 'Java', 'Python', 'Go'); echo 'origin:<br>'; print_r($fav_langs); echo '<br>'; $e = array_pop($fav_langs); // 删除末尾元素 echo 'Removed last: ' . $e . '<br>'; print_r($fav_langs); echo '<br>'; $e = array_shift($fav_langs); // 删除开头元素 echo 'Removed front: ' . $e . '<br>'; print_r($fav_langs); echo '<br>'; echo '<h2>Removing Elements from Associative Array</h2>'; $fav_colors = array('red'=>'#FF0000', 'green'=>'#00FF00', 'blue'=>'#0000FF'); echo 'origin:<br>'; print_r($fav_colors); echo '<br>'; $e = array_pop($fav_colors); // 删除末尾元素 echo 'Removed last: ' . $e . '<br>'; print_r($fav_colors); echo '<br>'; $e = array_shift($fav_colors); // 删除开头元素 echo 'Removed front: ' . $e . '<br>'; print_r($fav_colors); echo '<br>'; ?> </div> </body> </html>
查看运行结果
相关文章推荐
- PHP学习笔记(06)—— 数组
- php学习笔记:第五节--php数组
- 韩顺平_PHP程序员玩转算法公开课(第一季)06_堆栈在内存中存在形式剖析_学习笔记_源代码图解_PPT文档整理
- PHP学习笔记【12】--PHP数组排序
- PHP学习笔记(2)-重要的数组[]
- PHP学习笔记1 数组函数 array range array_pad array_splice count
- PHP学习笔记(3) 数组相关函数②.
- php学习笔记------[数组的常用函数]
- PHP学习笔记三:PHP数组学习小结
- PHP学习笔记-数组
- PHP学习笔记9:数组
- PHP学习笔记——使用array()声明数组
- php学习笔记(7):PHP数组的创建修改使用
- php学习笔记 数组的常用函数
- php学习笔记-数组篇(3)
- php 学习笔记--数组篇(5)
- 【小白笔记】PHP学习之路 (14) --数组(4)
- PHP学习笔记——PHP数组中键和值搜索相关函数3