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 数组实例说明
Aug 18 PHP
php checkbox复选框值的获取与checkbox默认值输出方法
May 15 PHP
php类的定义与继承用法实例
Jul 07 PHP
54个提高PHP程序运行效率的方法
Jul 19 PHP
php获取本机真实IP地址实例代码
Mar 31 PHP
php简单实现短网址(短链)还原的方法(测试可用)
May 09 PHP
Yii2中Restful API原理实例分析
Jul 25 PHP
解析 thinkphp 框架中的部分方法
May 07 PHP
基于php中echo用逗号和用点号的区别详解
Jan 23 PHP
Laravel 中使用简单的方法跟踪用户是否在线(推荐)
Oct 30 PHP
PHP 命名空间和自动加载原理与用法实例分析
Apr 29 PHP
Laravel中GraphQL接口请求频率实战记录
Sep 01 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
谈谈PHP语法(2)
2006/10/09 PHP
在PHP中实现Javascript的escape()函数代码
2010/08/08 PHP
php数组函数序列 之array_count_values() 统计数组中所有值出现的次数函数
2011/10/29 PHP
使用PHP下载CSS文件中的所有图片【几行代码即可实现】
2016/12/14 PHP
JS+PHP实现用户输入数字后显示最大的值及所在位置
2017/06/19 PHP
用javascript实现点击链接弹出&quot;图片另存为&quot;而不是直接打开
2007/08/15 Javascript
纯JavaScript实现HTML5 Canvas六种特效滤镜示例
2013/06/28 Javascript
JS保留两位小数 四舍五入函数的小例子
2013/11/20 Javascript
原生JS操作网页给p元素添加onclick事件及表格隔行变色
2013/12/01 Javascript
Jquery遍历节点的方法小集
2014/01/22 Javascript
用nodejs实现PHP的print_r函数代码
2014/03/14 NodeJs
淘宝网提供的国内NPM镜像简介和使用方法
2014/04/17 Javascript
JS实现双击编辑可修改状态的方法
2015/08/14 Javascript
JavaScript学习笔记之数组去重
2016/03/23 Javascript
分享一个精简的vue.js 图片lazyload插件实例
2017/03/13 Javascript
JS库之wow.js使用方法
2017/09/14 Javascript
vue生成token并保存到本地存储中
2018/07/17 Javascript
Layer弹出层动态获取数据的方法
2018/08/20 Javascript
对VUE中的对象添加属性
2018/09/18 Javascript
vue+koa2实现session、token登陆状态验证的示例
2019/08/30 Javascript
js blob类型url的视频下载问题的解决
2019/11/29 Javascript
详解javascript脚本何时会被执行
2021/02/05 Javascript
python中的错误处理
2016/04/10 Python
python监控linux内存并写入mongodb(推荐)
2017/09/11 Python
python中使用%与.format格式化文本方法解析
2017/12/27 Python
python链接oracle数据库以及数据库的增删改查实例
2018/01/30 Python
Python GUI Tkinter简单实现个性签名设计
2018/06/19 Python
Python并发之多进程的方法实例代码
2018/08/15 Python
python中cPickle类使用方法详解
2018/08/27 Python
python读取图片任意范围区域
2019/01/23 Python
python中时间模块的基本使用教程
2019/05/14 Python
Pytorch 抽取vgg各层并进行定制化处理的方法
2019/08/20 Python
学校学习雷锋活动总结
2014/07/03 职场文书
详解CSS不受控制的position fixed
2021/05/25 HTML / CSS
python中__slots__节约内存的具体做法
2021/07/04 Python
世界十大狙击步枪排行榜
2022/03/20 杂记