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 字符串操作入门教程
Dec 06 PHP
使用Xdebug调试和优化PHP程序之[1]
Apr 17 PHP
PHP 获取MSN好友列表的代码(2009-05-14测试通过)
Sep 09 PHP
PHP 递归效率分析
Nov 24 PHP
两千行代码的PHP学习笔记汇总
Oct 05 PHP
php表单敏感字符过滤类
Dec 08 PHP
Yii2 GridView实现列表页直接修改数据的方法
May 16 PHP
简单的pgsql pdo php操作类实现代码
Aug 25 PHP
php函数mkdir实现递归创建层级目录
Oct 27 PHP
php使用ftp实现文件上传与下载功能
Jul 21 PHP
php字符串过滤strip_tags()函数用法实例分析
Jun 24 PHP
PHP函数用法详解【初始化、嵌套、内置函数等】
Jun 02 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
一个显示某段时间内每个月的方法 返回由这些月份组成的数组
2012/05/16 PHP
解析php二分法查找数组是否包含某一元素
2013/05/23 PHP
php获取发送给用户的header信息的方法
2015/03/16 PHP
基于php的微信公众平台开发入门实例
2015/04/15 PHP
PHP 获取客户端 IP 地址的方法实例代码
2018/11/11 PHP
搭建pomelo 开发环境
2014/06/24 Javascript
面向切面编程(AOP)的理解
2015/05/01 Javascript
javascript中Array()数组函数详解
2015/08/23 Javascript
JavaScript如何动态创建table表格
2020/08/02 Javascript
JavaScript数据存储 Cookie篇
2016/07/02 Javascript
JavaScript从0开始构思表情插件
2016/07/26 Javascript
js数组去重的hash方法
2016/12/22 Javascript
纯js实现画一棵树的示例
2017/09/05 Javascript
JS求Number类型数组中最大元素方法
2018/04/08 Javascript
JS跨域请求的问题解析
2018/12/03 Javascript
详解如何理解vue的key属性
2019/04/14 Javascript
layer页面跳转,获取html子节点元素的值方法
2019/09/27 Javascript
react ant Design手动设置表单的值操作
2020/10/31 Javascript
Python实现决策树C4.5算法的示例
2018/05/30 Python
Windows下安装Scrapy
2018/10/17 Python
Python中一般处理中文的几种方法
2019/03/06 Python
python如何制作缩略图
2019/04/30 Python
Pandas0.25来了千万别错过这10大好用的新功能
2019/08/07 Python
python实现网页录音效果
2020/10/26 Python
CSS书写规范、顺序和命名规则
2014/03/06 HTML / CSS
Interhome丹麦:在线预订度假屋和公寓
2019/07/18 全球购物
Weblogic和WebSphere不同特点
2012/05/09 面试题
退休感言
2014/01/28 职场文书
学生安全教育材料
2014/02/14 职场文书
大学生志愿者活动总结
2014/06/27 职场文书
党员对照检查材料
2014/09/22 职场文书
党的群众路线教育实践活动对照检查材料(个人)
2014/09/24 职场文书
禁毒心得体会范文
2016/01/15 职场文书
祝福语集锦:朋友新店开业祝福语
2019/12/10 职场文书
JavaScript流程控制(分支)
2021/12/06 Javascript
JavaScript中reduce()的用法
2022/05/11 Javascript