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 相关文章推荐
一个用于mysql的数据库抽象层函数库
Oct 09 PHP
PHP 程序员应该使用的10个组件
Oct 31 PHP
PHP随机字符串生成代码(包括大小写字母)
Jun 24 PHP
php调用C代码的实现方法
Mar 11 PHP
浅析PHP编程中10个最常见的错误
Aug 08 PHP
ThinkPHP控制器间实现相互调用的方法
Oct 31 PHP
smarty内置函数foreach用法实例
Jan 22 PHP
WordPress中查询文章的循环Loop结构及用法分析
Dec 17 PHP
Smarty变量用法详解
May 11 PHP
PHP实现对文件锁进行加锁、解锁操作的方法
Jul 04 PHP
PHP实现求连续子数组最大和问题2种解决方法
Dec 26 PHP
php实现二叉树中和为某一值的路径方法
Oct 14 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
肝肠寸断了解下!盘点史上最伤心的十大动漫
2020/03/04 日漫
php网页后退不再出现过期
2007/03/08 PHP
php简单开启gzip压缩方法(zlib.output_compression)
2013/04/13 PHP
php中的动态调用实例分析
2015/01/07 PHP
PHP中使用break跳出多重循环代码实例
2015/01/21 PHP
PHP模板引擎Smarty中的保留变量用法分析
2016/04/11 PHP
PHP封装的XML简单操作类完整实例
2017/11/13 PHP
PHP有序表查找之二分查找(折半查找)算法示例
2018/02/09 PHP
javascript 原型模式实现OOP的再研究
2009/04/09 Javascript
jquery getScript动态加载JS方法改进详解
2012/11/15 Javascript
前端开发过程中浏览器版本的两种判定方法
2013/10/30 Javascript
js写的评论分页(还不错)
2013/12/23 Javascript
jquery和雅虎的yql服务实现天气预报服务示例
2014/02/08 Javascript
jquery拖动层效果插件用法实例分析(附demo源码)
2016/04/28 Javascript
Vue.js实例方法之生命周期详解
2017/07/03 Javascript
javaScript实现鼠标在文字上悬浮时弹出悬浮层效果
2020/04/12 Javascript
nodejs多版本管理总结
2018/04/03 NodeJs
vue2.0结合Element-ui实战案例
2019/03/06 Javascript
Javascript 关于基本类型和引用类型的个人理解
2019/11/01 Javascript
JS替换字符串中指定位置的字符(多种方法)
2020/05/28 Javascript
Ant Design Pro 之 ProTable使用操作
2020/10/31 Javascript
Python构造函数及解构函数介绍
2015/02/26 Python
python实现文本文件合并
2015/12/29 Python
Pandas透视表(pivot_table)详解
2019/07/22 Python
python脚本实现mp4中的音频提取并保存在原目录
2020/02/27 Python
Python 生成VOC格式的标签实例
2020/03/10 Python
django中嵌套的try-except实例
2020/05/21 Python
python集合的新增元素方法整理
2020/12/07 Python
教师网络培训感言
2014/03/09 职场文书
建筑工地文明标语
2014/10/09 职场文书
2014年办公室工作总结范文
2014/11/12 职场文书
简单的个人租房协议书范本
2014/11/26 职场文书
西安大雁塔导游词
2015/02/10 职场文书
敬老院义诊活动总结
2015/05/07 职场文书
当你焦虑迷茫时,请读读这6句话
2019/07/24 职场文书
Requests什么的通通爬不了的Python超强反爬虫方案!
2021/05/20 Python