search
Homephp教程php手册5种PHP创建数组的实例代码分享

在本文将数组的各种创建方式用PHP实例代码的方式分享给大家,感兴趣的朋友可以了解下

看这篇文章之前相信大家都已经看过PHP中文手册关于数组这一节的讲解了,怎么样呢,看懂了多少?至少我第一次阅读文档时是一头雾水,也许是因为在翻译的不够通俗易懂吧^_^!!这里UncleToo根据自己的经验,将数组的各种创建方式用PHP实例代码的方式分享给大家,希望对大家有些帮助(当然,,PHP文档还是要多看的)

1、使用array()创建数组

array()创建数组是我们在PHP开发过程中最常用到的一种方式,准确来说array()是一种结构而不是一个函数。

示例1:

复制代码 代码如下:


$number = array(1,3,5,7,9);
$color =array("red","blue","green");
$student = array("name",17)
?>


示例2:

复制代码 代码如下:


$language = array(1=>"PHP",3=>"JAVA",4=>"C");
$student = array("name"=>"张三","age"=>17)
?>


当然,数组里没有值也是允许的,即空数组:

复制代码 代码如下:


$result = array();
?>


2、使用compact()函数创建数组

PHP中compact()函数可以将一个或多个变量转换为数组

定义格式:

array compact(var1,var2...)

示例1:任何没有变量名与之对应的字符串都被略过。

复制代码 代码如下:


$firstname = "Peter";
$lastname = "Griffin";
$age = "38";
$result = compact("firstname", "lastname", "age");
print_r($result);
?>


输出结果:

复制代码 代码如下:


Array
(
[firstname] => Peter
[lastname] => Griffin
[age] => 38
)


示例2:使用没有对应变量名的字符串,以及一个变量名数组

复制代码 代码如下:


$firstname = "Peter";
$lastname = "Griffin";
$age = "38";
$name = array("firstname", "lastname");
$result = compact($name, "location", "age");
print_r($result);
?>


输出结果:

复制代码 代码如下:


Array
(
[firstname] => Peter
[lastname] => Griffin
[age] => 38
)


3、使用array_combine()函数创建数组

PHP中array_combine()函数可以将两个数组合并成一个新数组,其中的一个数组是键名,另一个数组的值为键值。

定义格式:

array array_combine(array1,array2)

示例

复制代码 代码如下:


$a1=array("a","b","c","d");
$a2=array("Cat","Dog","Horse","Cow");
print_r(array_combine($a1,$a2));
?>


输出结果:

Array ( [a] => Cat [b] => Dog [c] => Horse [d] => Cow )

注意:使用array_combine()函数时,两个参数必须有相同数目的元素。

4、使用range()函数创建数组

定义格式:

array range(first,second,step)

first:元素最小值

second:元素最大值

step:元素步长

下面是官方给的定义:该函数创建一个数组,包含从 first 到 second (包含 first 和 second)之间的整数或字符。如果 second 比 first 小,则返回反序的数组。

理解起来比较吃力,我们直接看例子(本人就喜欢看有例子的教程)。

示例1:

复制代码 代码如下:


$number = range(0,5);
print_r ($number);
?>


输出结果:

复制代码 代码如下:


Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
)


示例2:

复制代码 代码如下:


$number = range(0,50,10);
print_r ($number);
?>


输出结果:

复制代码 代码如下:


Array
(
[0] => 0
[1] => 10
[2] => 20
[3] => 30
[4] => 40
[5] => 50
)


示例3:

复制代码 代码如下:


$letter = range("a","d");
print_r ($letter);
?>


输出结果:

复制代码 代码如下:


Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)


5、使用array_fill()函数创建数组

array_fill()函数是用给定的值类填充数组

定义格式:

array_fill(start,number,value)

start:起始索引

number:数组个数

value:数组值

示例:

复制代码 代码如下:


$a=array_fill(2,3,"Dog");
print_r($a);
?>


输出结果:

Array ( [2] => Dog [3] => Dog [4] => Dog )
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.