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时间戳使用实例代码
Jun 07 PHP
php 购物车的例子
May 04 PHP
PHP漏洞全解(详细介绍)
Nov 13 PHP
php动态绑定变量的用法
Jun 16 PHP
php给图片添加文字水印方法汇总
Aug 27 PHP
PHP中ltrim与rtrim去除左右空格及特殊字符实例
Jan 07 PHP
php中使用GD库做验证码
Mar 31 PHP
PHP使用SOAP扩展实现WebService的方法
Apr 01 PHP
对php 判断http还是https,以及获得当前url的方法详解
Jan 15 PHP
laravel实现简单用户权限的示例代码
May 28 PHP
php pdo连接数据库操作示例
Nov 18 PHP
laravel框架分组控制器和分组路由实现方法示例
Jan 25 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
全国FM电台频率大全 - 6 辽宁省
2020/03/11 无线电
PHP5中的时间相差8小时的解决办法
2008/03/28 PHP
PHP利用header跳转失效的解决方法
2014/10/24 PHP
PHP使用ob_start生成html页面的方法
2014/11/07 PHP
PHP中COOKIES使用示例
2015/07/26 PHP
php使用APC实现实时上传进度条功能
2015/10/26 PHP
phpStudy访问速度慢和启动失败的解决办法
2015/11/19 PHP
PHP通过引用传递参数用法分析
2016/12/01 PHP
Laravel框架集合用法实例浅析
2020/05/14 PHP
JQuery使用$.ajax和checkbox实现下次不在通知功能
2015/04/16 Javascript
js实现ctrl+v粘贴上传图片(兼容chrome、firefox、ie11)
2016/03/09 Javascript
jQuery原理系列-常用Dom操作详解
2016/06/07 Javascript
实现easyui的datagrid导出为excel的示例代码
2016/11/10 Javascript
jQuery学习之DOM节点的插入方法总结
2017/01/22 Javascript
详解Node.js 命令行程序开发教程
2017/06/07 Javascript
微信小程序基于slider组件动态修改标签透明度的方法示例
2017/12/04 Javascript
react redux入门示例
2018/04/19 Javascript
js中数组对象去重的两种方法
2019/01/18 Javascript
vue-router实现嵌套路由的讲解
2019/01/19 Javascript
详解Vue webapp项目通过HBulider打包原生APP(vue+webpack+HBulider)
2019/02/02 Javascript
koa大型web项目中使用路由装饰器的方法示例
2019/04/02 Javascript
一篇文章,教你学会Vue CLI 插件开发
2019/04/17 Javascript
基于VUE实现判断设备是PC还是移动端
2020/07/03 Javascript
[57:31]DOTA2-DPC中国联赛 正赛 SAG vs CDEC BO3 第一场 2月1日
2021/03/11 DOTA
用Python写的图片蜘蛛人代码
2012/08/27 Python
简单介绍Python中的readline()方法的使用
2015/05/24 Python
python的keyword模块用法实例分析
2015/06/30 Python
python操作redis方法总结
2018/06/06 Python
Django的models中on_delete参数详解
2019/07/16 Python
解决pyinstaller 打包exe文件太大,用pipenv 缩小exe的问题
2020/07/13 Python
捐助倡议书范文
2014/04/15 职场文书
大学生应聘求职信
2014/05/26 职场文书
火烧圆明园观后感
2015/06/03 职场文书
幼儿园园长新年寄语
2015/08/17 职场文书
2016孝老爱亲模范事迹材料
2016/02/26 职场文书
CSS3实现的3D隧道效果
2021/04/27 HTML / CSS