php数组总结篇(一)


Posted in PHP onSeptember 30, 2008

数组
1.数组的下标是整型数值或者是字符串类型。
eg1.索引数组的键是______,关联数组的键是______。
2.字符串作为索引的时候,应加上引号。常量或者变量不用加引号,否则无法编译。
在php中,没有引号的字符串会自动生成一个裸字符串,而 PHP 可能会在以后定义此常量,不幸的是你的代码中有同样的名字,那么这个字符串就被重新赋值。
eg2.<?php
// 显示所有错误
error_reporting(E_ALL);
$arr = array('fruit' => 'apple', 'veggie' => 'carrot');
// 正确
print $arr['fruit']; // apple
print $arr['veggie']; // carrot
// 不正确。This works but also throws a PHP error of
// level E_NOTICE because of an undefined constant named fruit
//
// Notice: Use of undefined constant fruit - assumed 'fruit' in...
print $arr[fruit]; // apple
// Let's define a constant to demonstrate what's going on. We
// will assign value 'veggie' to a constant named fruit.
define('fruit','veggie');
// Notice the difference now
print $arr['fruit']; // apple
print $arr[fruit]; // carrot
// The following is okay as it's inside a string. Constants are not
// looked for within strings so no E_NOTICE error here
print "Hello $arr[fruit]"; // Hello apple
// With one exception, braces surrounding arrays within strings
// allows constants to be looked for
print "Hello {$arr[fruit]}"; // Hello carrot
print "Hello {$arr['fruit']}"; // Hello apple
// This will not work, results in a parse error such as:
// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'
// This of course applies to using autoglobals in strings as well
print "Hello $arr['fruit']";
print "Hello $_GET['foo']";
// Concatenation is another option
print "Hello " . $arr['fruit']; // Hello apple
?>
3.键值问题
$a['color'] = 'red';
$a['taste'] = 'sweet';
$a['shape'] = 'round';
$a['name'] = 'apple';
$a[] = 4; // key will be 0
$b[] = 'a'; // key will be 0
$b[] = 'b'; // key will be 1
$b[] = 'c'; // key will be 2
switching = array( 10, // key = 0
5 => 6,
3 => 7,
'a' => 4,
11, // key = 6 (maximum of integer-indices was 5)
'8' => 2, // key = 8 (integer!)
'02' => 77, // key = '02'
0 => 12 // the value 10 will be overwritten by 12
);
<?php
$multi_array = array("red",
"green",
42 => "blue","yellow" => array("apple",9 => "pear","banana","orange" => array("dog","cat","iguana")));
?>
A.$multi_array['yellow']['apple'][0]
B.$multi_array['blue'][0]['orange'][1]
C.$multi_array[3][3][2]
D.$multi_array['yellow']['orange']['cat']
E.$multi_array['yellow']['orange'][1]
--------------------------------待续待续待续------
4.array_walk
5.var_dump
6.array_intersect
7.array_sum
8.array_count_values
9.array_flip
10.natsort
11.ksort(),asort(),krsort(),sort(),usort()
12.array_reverse()
13.array_merge
14.reset
-------------------------------待续待续待续------
15.array_combine
16array_count_values
17.array_diff
18.array_filter
19.array_search

PHP 相关文章推荐
使用PHP socke 向指定页面提交数据
Jul 23 PHP
php学习之 循环结构实现代码
Jun 09 PHP
smarty模板局部缓存方法使用示例
Jun 17 PHP
Session 失效的原因汇总及解决丢失办法
Sep 30 PHP
PHP安装GeoIP扩展根据IP获取地理位置及计算距离的方法
Jul 01 PHP
PHP+JQuery+Ajax实现分页方法详解
Aug 06 PHP
thinkphp利用模型通用数据编辑添加和删除的实例代码
Nov 20 PHP
php的laravel框架快速集成微信登录的方法
Dec 12 PHP
PHP运用foreach神奇的转换数组(实例讲解)
Feb 01 PHP
PHP 中 var_export、print_r、var_dump 调试中的区别
Jun 19 PHP
PHP实现函数内修改外部变量值的方法示例
Dec 28 PHP
Thinkphp 框架基础之入口文件功能、定义与用法分析
Apr 27 PHP
PHP EOT定界符的使用详解
Sep 30 #PHP
40个迹象表明你还是PHP菜鸟
Sep 29 #PHP
PHP网站基础优化方法小结
Sep 29 #PHP
10条PHP编程习惯助你找工作
Sep 29 #PHP
PHP生成带有雪花背景的验证码
Sep 28 #PHP
PHP编实现程动态图像的创建代码
Sep 28 #PHP
php 三维饼图的实现代码
Sep 28 #PHP
You might like
PHP ob缓存以及ob函数原理实例解析
2020/11/13 PHP
一实用的实现table排序的Javascript类库
2007/09/12 Javascript
JavaScript 继承详解(二)
2009/07/13 Javascript
JavaScript中的字符串操作详解
2013/11/12 Javascript
在javascript中如何得到中英文混合字符串的长度
2014/01/17 Javascript
在JavaScript中操作时间之getMonth()方法的使用
2015/06/10 Javascript
举例讲解JavaScript中将数组元素转换为字符串的方法
2015/10/25 Javascript
JavaScript的React框架中的JSX语法学习入门教程
2016/03/05 Javascript
xtemplate node.js 的使用方法实例解析
2016/08/22 Javascript
js遍历获取表格内数据的方法(必看)
2017/04/06 Javascript
详解AngularJS 模块化
2017/06/14 Javascript
详谈js原型继承的一些问题
2017/09/06 Javascript
Angular使用Md5加密的解决方法
2017/09/16 Javascript
JS实现将二维数组转为json格式字符串操作示例
2018/07/12 Javascript
Vue中CSS动画原理的实现
2019/02/13 Javascript
javascript+Canvas实现画板功能
2020/06/23 Javascript
vue cli 3.0通用打包配置代码,不分一二级目录
2020/09/02 Javascript
[36:52]DOTA2真视界:基辅特锦赛总决赛
2017/05/21 DOTA
Python中函数的多种格式和使用实例及小技巧
2015/04/13 Python
Python2.x中文乱码问题解决方法
2015/06/02 Python
Django实现的自定义访问日志模块示例
2017/06/23 Python
django 删除数据库表后重新同步的方法
2018/05/27 Python
详解关于Django中ORM数据库迁移的配置
2018/10/08 Python
PyQt5 QTable插入图片并动态更新的实例
2019/06/18 Python
python读取.mat文件的数据及实例代码
2019/07/12 Python
Html5 canvas实现粒子时钟的示例代码
2018/09/06 HTML / CSS
西部世纪面试题
2014/12/05 面试题
交通事故赔偿协议书
2014/04/15 职场文书
高中教师评语大全
2014/04/25 职场文书
保证书格式
2015/01/16 职场文书
交通事故案件代理词
2015/05/23 职场文书
严以律己专题学习研讨会发言材料
2015/11/09 职场文书
《静夜思》教学反思
2016/02/17 职场文书
职工趣味运动会开幕词
2016/03/04 职场文书
《西游记》读后感(3篇)
2019/09/20 职场文书
用 Python 元类的特性实现 ORM 框架
2021/05/19 Python