PHP数组操作汇总

对于Web编程来说,最重要的就是存取和读写数据了。存储方式可能有很多种,可以是字符串、数组、文件的形式等。数组,可以说是PHP的数据应用中较重要的一种方式。PHP的数组函数众多,下面是我学习的小结,借此记之,便于以后鉴之。

compact()

compact()函数——将一个或多个变量(包含数组)转换为数组:array compact ( mixed $varname [, mixed $... ] )。

<?PHP $number = "1,3,5,7,9"; $string = "I'm PHPer"; $array = array("And","You?"); $newArray = compact("number","string","array"); print_r ($newArray); ?> compact()函数用于将两个或多个变量转换为数组,当然也包含数组变量。其参数是变量的名称而非带有$全名。相反的函数是extract()作用顾名思义就是将数组转换为单个的字符串,键值作为其字符串名称,数组值作为字符串的值。

Array ( [number] => 1,3,5,7,9 [string] => I'm PHPer [array] => Array ( [0] => And [1] => You? ) )

array_combine()

array_combine()——将两个数组重组成一个数组,一个作键值一个做的值:array array_combine ( array $keys , array $values )

<?PHP $number = array("1","3","5","7","9"); $array = array("I","Am","A","PHP","er"); $newArray = array_combine($number,$array); print_r ($newArray); ?>

array_combine函数不多说了,谁看了都明白。

运行结果:

Array ( [1] => I [3] => Am [5] => A [7] => PHP [9] => er )

range()

range()函数——创建指定范围的数组:

<?PHP $array1 = range(0,100,10);//0为起始值,100为结束值,10为步进值(默认步进值为1). print_r($array1); echo"<br />"; $array2 = range("A","Z"); print_r($array2); echo "<br />"; $array3 = range("z","a"); print_r($array3); ?>

array_fill()

array_fill()函数——填充数组函数:

<?PHP $array = range(1,10); $fillarray = range("a","d"); $arrayFilled = array_fill(0,5,$fillarray);//这里的$fillarray可以是字符串,如"test". echo "<pre>"; print_r ($arrayFilled); echo "</pre>"; $keys = array("string","2",9,"SDK","PK"); $array2 = array_fill_keys($keys,"testing"); echo "<pre>"; print_r ($array2); echo "</pre>"; ?> 运行结果:

Array ( [0] => Array ( [0] => a [1] => b [2] => c [3] => d ) [1] => Array ( [0] => a [1] => b [2] => c [3] => d ) [2] => Array ( [0] => a [1] => b [2] => c [3] => d ) [3] => Array ( [0] => a [1] => b [2] => c [3] => d ) [4] => Array ( [0] => a [1] => b [2] => c [3] => d ) ) Array ( [string] => testing [2] => testing [9] => testing [SDK] => testing [PK] => testing )

原文发布于微信公众号 - php(phpdaily)

原文发表时间:2015-08-26

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏码匠的流水账

聊聊NettyConnector的start及shutdown

reactor-netty-0.7.6.RELEASE-sources.jar!/reactor/ipc/netty/NettyConnector.java

841
来自专栏飞扬的花生

jsencrypt参数前端加密c#解密

      写程序时一般是通过form表单或者ajax方式将参数提交到服务器进行验证,如何防止提交的请求不被抓包后串改,虽然无法说绝对安全却给非法提交提高了难度...

3849
来自专栏一个会写诗的程序员的博客

Spring Reactor 项目核心库Reactor Core

Non-Blocking Reactive Streams Foundation for the JVM both implementing a Reactiv...

2092
来自专栏转载gongluck的CSDN博客

cocos2dx 打灰机

#include "GamePlane.h" #include "PlaneSprite.h" #include "BulletNode.h" #include...

5196
来自专栏张善友的专栏

Miguel de Icaza 细说 Mix 07大会上的Silverlight和DLR

Mono之父Miguel de Icaza 详细报道微软Mix 07大会上的Silverlight和DLR ,上面还谈到了Mono and Silverligh...

2657
来自专栏菩提树下的杨过

Flash/Flex学习笔记(23):运动学原理

先写一个公用的小球类Ball: package{ import flash.display.Sprite; //小球 类 public class B...

25110
来自专栏张善友的专栏

Mix 10 上的asp.net mvc 2的相关Session

Beyond File | New Company: From Cheesy Sample to Social Platform Scott Hansel...

2517
来自专栏张善友的专栏

LINQ via C# 系列文章

LINQ via C# Recently I am giving a series of talk on LINQ. the name “LINQ via C...

2605
来自专栏我和未来有约会

Kit 3D 更新

Kit3D is a 3D graphics engine written for Microsoft Silverlight. Kit3D was inita...

2486
来自专栏大内老A

The .NET of Tomorrow

Ed Charbeneau(http://developer.telerik.com/featured/the-net-of-tomorrow/) Exciti...

30510

扫码关注云+社区