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概述.
Oct 09 PHP
PHP DataGrid 实现代码
Aug 12 PHP
PHP CKEditor 上传图片实现代码
Nov 06 PHP
ThinkPHP中的create方法与自动令牌验证实例教程
Aug 22 PHP
ThinkPHP实例化模型的四种方法概述
Aug 22 PHP
PHP自动重命名文件实现方法
Nov 04 PHP
PHP采用curl模仿用户登陆新浪微博发微博的方法
Nov 07 PHP
thinkphp文件处理类Dir.class.php的用法分析
Dec 08 PHP
Apache PHP MySql安装配置图文教程
Aug 27 PHP
基于ThinkPHP实现的日历功能实例详解
Apr 15 PHP
Laravel框架下的Contracts契约详解
Mar 17 PHP
PHP接口类(interface)的定义、特点和应用示例
May 18 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
对Session和Cookie的区分与解释
2007/03/16 PHP
Laravel4中的Validator验证扩展用法详解
2016/07/26 PHP
TP5框架安全机制实例分析
2020/04/05 PHP
javascript 写类方式之五
2009/07/05 Javascript
Whatever:hover 无需javascript让IE支持丰富伪类
2010/06/29 Javascript
jQuery实现加入购物车飞入动画效果
2015/03/14 Javascript
JavaScript对表格或元素按文本,数字或日期排序的方法
2015/05/26 Javascript
js实现文字垂直滚动和鼠标悬停效果
2015/12/31 Javascript
使用jQuery制作遮罩层弹出效果的极简实例分享
2016/05/12 Javascript
基于jQuery的Web上传插件Uploadify使用示例
2016/05/19 Javascript
vue实现在表格里,取每行的id的方法
2018/03/09 Javascript
node打造微信个人号机器人的方法示例
2018/04/26 Javascript
微信小程序实践之动态控制组件的显示/隐藏功能
2018/07/18 Javascript
AngularJS ui-router刷新子页面路由的方法
2018/07/23 Javascript
ES6 Symbol数据类型的应用实例分析
2019/06/26 Javascript
解决layui laydate 时间控件一闪而过的问题
2019/09/28 Javascript
基于Vue实现微前端的示例代码
2020/04/24 Javascript
通过实例解析chrome如何在mac环境中安装vue-devtools插件
2020/07/10 Javascript
python实现一次创建多级目录的方法
2015/05/15 Python
Python的Django框架安装全攻略
2015/07/15 Python
Linux下通过python访问MySQL、Oracle、SQL Server数据库的方法
2016/04/23 Python
Python基础知识_浅谈用户交互
2017/05/31 Python
pyqt5利用pyqtDesigner实现登录界面
2019/03/28 Python
pytorch之添加BN的实现
2020/01/06 Python
numpy中生成随机数的几种常用函数(小结)
2020/08/18 Python
python实现sm2和sm4国密(国家商用密码)算法的示例
2020/09/26 Python
详解matplotlib中pyplot和面向对象两种绘图模式之间的关系
2021/01/22 Python
意大利体育用品网上商城:Nencini Sport
2016/08/18 全球购物
波兰最大的儿童服装连锁店之一:5.10.15.
2018/02/11 全球购物
美国用餐电影院:Alamo Drafthouse Cinema
2020/01/23 全球购物
服务之星获奖感言
2014/01/21 职场文书
小学生暑假感言
2014/02/06 职场文书
项目经理聘任书
2014/03/29 职场文书
幼儿园校车安全责任书
2015/05/08 职场文书
给朋友的道歉短信
2015/05/12 职场文书
小学秋季运动会加油口号及加油稿
2019/08/19 职场文书