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程序员编程注意事项
Apr 10 PHP
防止MySQL注入或HTML表单滥用的PHP程序
Jan 21 PHP
php 无限分类的树类代码
Dec 03 PHP
PHP PDO函数库详解
Apr 27 PHP
PHP 如何获取二维数组中某个key的集合
Jun 03 PHP
ioncube_loader_win_5.2.dll的错误解决方法
Jan 04 PHP
支持png透明图片的php生成缩略图类分享
Feb 08 PHP
PHP中如何防止外部恶意提交调用ajax接口
Apr 11 PHP
php中10个不同等级压缩优化图片操作示例
Nov 14 PHP
PHP数字前补0的自带函数sprintf 和number_format的用法(详解)
Feb 06 PHP
php设计模式之状态模式实例分析【星际争霸游戏案例】
Mar 26 PHP
PHP7修改的函数
Mar 09 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适配器模式介绍
2012/08/14 PHP
PHP中exec函数和shell_exec函数的区别
2014/08/20 PHP
CentOS下PHP安装Oracle扩展
2015/02/15 PHP
PHP封装的PDO数据库操作类实例
2017/06/21 PHP
PHP编程中的Session阻塞问题与解决方法分析
2017/08/07 PHP
iframe 父窗口和子窗口相互的调用方法集锦
2010/12/15 Javascript
Jquery插件easyUi表单验证提交(示例代码)
2013/12/30 Javascript
JavaScript实现继承的4种方法总结
2014/10/16 Javascript
jQuery移动页面开发中的触摸事件与虚拟鼠标事件简介
2015/12/03 Javascript
JavaScript正则表达式校验与递归函数实际应用实例解析
2017/08/04 Javascript
解决在Bootstrap模糊框中使用WebUploader的问题
2018/03/22 Javascript
jQuery实现的手动拖动控制进度条效果示例【测试可用】
2018/04/18 jQuery
vue.js 输入框输入值自动过滤特殊字符替换中问标点操作
2020/08/31 Javascript
vue 计算属性和侦听器的使用小结
2021/01/25 Vue.js
Python 序列化 pickle/cPickle模块使用介绍
2014/11/30 Python
Python warning警告出现的原因及忽略方法
2020/01/31 Python
TensorFlow获取加载模型中的全部张量名称代码
2020/02/11 Python
如何在windows下安装Pycham2020软件(方法步骤详解)
2020/05/03 Python
运行Python编写的程序方法实例
2020/10/21 Python
Python实现异步IO的示例
2020/11/05 Python
html5 视频播放解决方案
2016/11/06 HTML / CSS
浅谈amaze-ui中datepicker和datetimepicker注意的几点
2020/08/21 HTML / CSS
欧舒丹英国官网:购买欧舒丹护手霜等明星产品
2017/01/17 全球购物
英国最大的LED专业零售商:Led Hut
2018/03/16 全球购物
德国最大的婴儿用品网上商店:Kidsroom.de(支持中文)
2020/09/02 全球购物
后勤岗位职责
2013/11/26 职场文书
个性车贴标语
2014/06/24 职场文书
授权委托书公证
2014/09/14 职场文书
2014年四风个人对照检查及整改措施
2014/10/28 职场文书
2014年企业工会工作总结
2014/11/12 职场文书
2014年妇委会工作总结
2014/12/10 职场文书
2016高一新生军训心得体会
2016/01/11 职场文书
redis客户端实现高可用读写分离的方式详解
2021/07/04 Redis
opencv用VS2013调试时用Image Watch插件查看图片
2021/07/26 Python
vue ref如何获取子组件属性值
2022/03/31 Vue.js
Spring Boot 底层原理基础深度解析
2022/04/03 Java/Android