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 adodb连接mssql解决乱码问题
Jun 12 PHP
elgg 获取文件图标地址的方法
Mar 20 PHP
php截取utf-8中文字符串乱码的解决方法
Mar 29 PHP
php截取中文字符串不乱码的方法
Dec 25 PHP
php过滤HTML标签、属性等正则表达式汇总
Sep 22 PHP
PHP字符串word末字符实现大小写互换的方法
Nov 10 PHP
php文档工具PHP Documentor安装与使用方法
Jan 25 PHP
php发送http请求的常用方法分析
Nov 08 PHP
thinkPHP数据库增删改查操作方法实例详解
Dec 06 PHP
PHP异常处理定义与使用方法分析
Jul 25 PHP
Laravel框架FormRequest中重写错误处理的方法
Feb 18 PHP
PHP设计模式之建造者模式(Builder)原理与用法案例详解
Dec 12 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
Yii使用ajax验证显示错误messagebox的解决方法
2014/12/03 PHP
Zend Framework教程之动作的基类Zend_Controller_Action详解
2016/03/07 PHP
PHP实现的简单适配器模式示例
2017/06/22 PHP
gearman中worker常驻后台,导致MySQL server has gone away的解决方法
2020/02/27 PHP
TP5框架实现一次选择多张图片并预览的方法示例
2020/04/04 PHP
JavaScript 垃圾回收机制分析
2013/10/10 Javascript
jquery实现仿Flash的横向滑动菜单效果代码
2015/09/17 Javascript
JavaScript转换与解析JSON方法实例详解
2015/11/24 Javascript
JAVASCRIPT代码编写俄罗斯方块网页版
2015/11/26 Javascript
深入JavaScript高级程序设计之对象、数组(栈方法,队列方法,重排序方法,迭代方法)
2015/12/01 Javascript
详解JavaScript正则表达式之RegExp对象
2015/12/13 Javascript
Javascript创建类和对象详解
2017/05/31 Javascript
JavaScript初学者必看“new”
2017/06/12 Javascript
vue项目中,main.js,App.vue,index.html的调用方法
2018/09/20 Javascript
vue 集成 vis-network 实现网络拓扑图的方法
2019/08/07 Javascript
JS使用Chrome浏览器实现调试线上代码
2020/07/23 Javascript
用实例分析Python中method的参数传递过程
2015/04/02 Python
Python中列表元素转为数字的方法分析
2016/06/14 Python
用Python实现二叉树、二叉树非递归遍历及绘制的例子
2019/08/09 Python
python实现秒杀商品的微信自动提醒功能(代码详解)
2020/04/27 Python
AVIS安飞士奥地利租车官网:提供奥地利、欧洲和全世界汽车租赁
2016/11/29 全球购物
亚历山大·王官网:Alexander Wang
2017/06/23 全球购物
MADE法国:提供原创设计师家具
2018/09/18 全球购物
工商管理专业应届生求职信
2013/11/04 职场文书
广告设计应届生求职信
2014/03/01 职场文书
政府门卫岗位职责
2014/04/29 职场文书
新店开张活动方案
2014/08/24 职场文书
药店促销活动策划方案
2014/08/24 职场文书
公司聚餐通知
2015/04/22 职场文书
2015年行政人事部工作总结
2015/05/13 职场文书
毕业论文致谢信
2015/05/14 职场文书
餐厅开业活动方案
2019/07/08 职场文书
创业计划书之川味火锅店
2019/09/02 职场文书
css实现文章分割线样式的多种方法总结
2021/04/21 HTML / CSS
你喜欢篮球吗?Python实现篮球游戏
2021/06/11 Python
《乙女游戏世界对路人角色很不友好》OP主题曲无字幕动画MV公开
2022/04/05 日漫