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与javascript的两种交互方式
Oct 09 PHP
PHP学习之数组值的操作
Apr 17 PHP
PHP爆绝对路径方法收集整理
Sep 17 PHP
使用PHP静态变量当缓存的方法
Nov 13 PHP
PHP输入流php://input实例讲解
Dec 22 PHP
PHP实现数据分页显示的简单实例
May 26 PHP
Yii 2.0实现联表查询加搜索分页的方法示例
Aug 02 PHP
PHP实现的最大正向匹配算法示例
Dec 19 PHP
Laravel 对某一列进行筛选然后求和sum()的例子
Oct 10 PHP
PHP替换Word中变量并导出PDF图片的实现方法
Nov 26 PHP
php中pcntl_fork详解
Apr 01 PHP
laravel ajax curd 搜索登录判断功能的实现
Apr 17 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 db类库进行数据库操作
2009/03/19 PHP
php中计算未知长度的字符串哪个字符出现的次数最多的代码
2012/08/14 PHP
php遍历所有文件及文件夹的方法深入解析
2013/06/08 PHP
探讨:parse url解析URL,返回其组成部分
2013/06/14 PHP
领悟php接口中interface存在的意义
2013/06/27 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(八)
2014/06/23 PHP
PHP PDO fetch 模式各种参数的输出结果一览
2015/01/07 PHP
Laravel事件监听器用法实例分析
2019/03/12 PHP
文字幻灯片
2006/06/26 Javascript
幻宇的层模拟窗口效果-提供演示和下载
2007/01/20 Javascript
jQuery function的正确书写方法
2013/08/02 Javascript
JS中的this变量的使用介绍
2013/10/21 Javascript
jquery.post用法之type设置问题
2014/02/24 Javascript
javascript面向对象特性代码实例
2014/06/12 Javascript
jquery实现保存已选用户
2014/07/21 Javascript
分享有关jQuery中animate、slide、fade等动画的连续触发、滞后反复执行的bug
2016/01/10 Javascript
jQuery实现点击某个div打开层,点击其他div关闭层实例分析(阻止冒泡)
2016/11/18 Javascript
前端自动化开发之Node.js的环境搭建教程
2017/04/01 Javascript
vue.js动态数据绑定学习笔记
2017/05/19 Javascript
js canvas实现适用于移动端的百分比仪表盘dashboard
2017/07/18 Javascript
解决vue中对象属性改变视图不更新的问题
2018/02/23 Javascript
js中Generator函数的深入讲解
2019/04/07 Javascript
js实现的订阅发布者模式简单示例
2020/03/14 Javascript
Django如何配置mysql数据库
2018/05/04 Python
详解Python locals()的陷阱
2019/03/26 Python
Linux下通过python获取本机ip方法示例
2019/09/06 Python
Python 使用type来定义类的实现
2019/11/19 Python
Python3 ID3决策树判断申请贷款是否成功的实现代码
2020/05/21 Python
python中plt.imshow与cv2.imshow显示颜色问题
2020/07/16 Python
纯CSS实现聊天框小尖角、气泡效果
2014/04/04 HTML / CSS
使用分层画布来优化HTML5渲染的教程
2015/05/08 HTML / CSS
YII2 全局异常处理深入讲解
2021/03/24 PHP
网络营销策划方案
2014/06/04 职场文书
2014年煤矿工人工作总结
2014/12/08 职场文书
公司年夜饭通知
2015/04/25 职场文书
Windows server 2022创建创建林、域树、子域的步骤
2022/06/25 Servers