search
php array basicsDec 02, 2016 am 09:49 AM
phpphp array

1. How to define an array: There are two main ways to create an array in PHP. Let’s take a look at how to create an array

(1) Create an array by directly assigning a value to each element.

The format is: $arrayname[key]=value;

where arrayname is the name of the array, key is the key of the array element, and value is the value of the element. The key can be a number such as 0, 1, 2, or 3, or a string. As shown below:
The code is as follows:

1 <?php 
2 //用1,2,3的数值作为数组的键 
3 echo &#39;<p>数组$array1的键值为:</p>&#39;; 
4 $array1[1]=&#39;a&#39;; 
5 $array1[2]=&#39;b&#39;; 
6 $array1[3]=&#39;c&#39;; 
7 print_r($array1); 
8 
9 //如果省略键的方式,则数组默认的键为从0开始递增的数值 
10 echo &#39;<p>数组$array2的键值为:</p>&#39;; 
11 $array2[]=&#39;a&#39;; 
12 $array2[]=&#39;b&#39;; 
13 $array2[]=&#39;c&#39;; 
14 print_r($array2); 
15 
16 //以字符串作为数组的键 
17 echo &#39;<p>数组$array3的键值为:</p>&#39;; 
18 $array3[&#39;one&#39;]=&#39;a&#39;; 
19 $array3[&#39;two&#39;]=&#39;b&#39;; 
20 $array3[&#39;three&#39;]=&#39;c&#39;; 
21 print_r($array3); 
22 ?>

The output result of the above code is:

The key value of array $array1 is:

Array ( [1] => a [2] => b [3] => c )
The key value of array $array2 is:

Array ( [0] => a [1] => b [2] => c )
The key value of array $array3 is:

Array ([one] => a [two] => b [three] => c )

(2) Use the array function to directly define an array.

The format is: $arrayname=array(key1=>value1, key2=>value2);

where arrayname is the name of the array, key1 and key2 are the keys of the array, and value1 and value2 correspond to the values ​​of key1 and key2 respectively.

Give an example, such as the following code:
The code is as follows:

1 <?php 
2 //以数值作为键 
3 $array6=array(1=>&#39;a&#39;,2=>&#39;b&#39;,3=>&#39;c&#39;); 
4 echo &#39;<p>数组$array6的键和值为:</p>&#39;; 
5 print_r($array6); 
6 //以字符串作为键 
7 $array7=array(&#39;one&#39;=>&#39;a&#39;,&#39;two&#39;=>&#39;b&#39;,&#39;three&#39;=>&#39;c&#39;); 
8 echo &#39;<p>数组$array7的键和值为:</p>&#39;; 
9 print_r($array7); 
10 //省略键的写法 
11 $array8=array(&#39;a&#39;,&#39;b&#39;,&#39;c&#39;); 
12 echo &#39;<p>数组$array8的键和值为:</p>&#39;; 
13 print_r($array8); 
14 ?>

The result is:

The keys and values ​​of array $array6 are:

Array ( [1] => a [2] => b [3] => c )
The keys and values ​​of array $array7 are:

Array ( [one] => a [two] => b [three] => c )
The keys of array $array8 The key and value are:

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

Note:

1>If you specify an element for an array If a value is used as its key, the default key of all elements after this element will be the self-increasing, non-repeating value of the specified value.

It’s a bit difficult to understand simply by looking at the literal meaning. Let’s take a look at an example:

The following code:
The code is as follows:

1 <?php 
2 //数组$array4第一个元素的键显示指定为2,之后的第2、3个元素以省略键的方式 
3 $array4[2]=&#39;a&#39;; 
4 $array4[]=&#39;b&#39;; 
5 $array4[]=&#39;c&#39;; 
6 //第4个元素的键显示指定为10,之后的第5、6个元素以省略键的方式 
7 $array4[10]=&#39;d&#39;; 
8 $array4[]=&#39;e&#39;; 
9 $array4[]=&#39;f&#39;; 
10 //第7个元素的键显示指定为9,之后的第8、9个元素以省略键的方式 
11 $array4[9]=&#39;g&#39;; 
12 $array4[]=&#39;h&#39;; 
13 $array4[]=&#39;i&#39;; 
14 //打印数组的键与值 
15 print_r($array4); 
16 ?>

The result is:

Array ( [2] => a [3] = > b [4] => c [10] => d [11] => e [12] => f [9] => g [13] => h [14] => ; i )

Explanation: The key of the seventh element is 9. Normally the key of the eighth element should be 10, but keys 10, 11 and 12 have been used by elements before, so the key of the eighth element is 13.

2>Whether a number or a string is used as the key of an array element, it only represents the key of this element and has no direct relationship with the position of this element in the array. This is different from C# and other languages. The biggest difference between arrays. Here's an example.

The following code:
The code is as follows:

1 <?php 
2 $array5[&#39;one&#39;]=&#39;a&#39;; 
3 if(!isset($array5[0])) 
4 { 
5 echo &#39;<p>$array5[0]是空的!</p>&#39;; 
6 } 
7 ?>

The result is:

$array5[0] is empty!

Explanation: $array5[0] represents the value of the element in the array whose key is the value 0 (it does not represent the first element of the array like C# and other languages), because the array only has the key string 'on' For this element, no element has a key of 0, so $array5[0] is empty.

>PHP supports two types of arrays: indexed array and associative array. The former uses numbers as keys, and the latter uses strings as keys. You can use a mix of numbers and strings as keys for elements when creating an array. The code is as follows:
The code is as follows:

1 <?php 
2 $array9=array(1=>&#39;a&#39;, 2=>&#39;b&#39;, &#39;one&#39;=>&#39;c&#39;, &#39;two&#39;=>&#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;); 
3 echo &#39;<p>数组$array9的键和值为:</p>&#39;; 
4 print_r($array9); 
5 ?>

The result is:

The keys and values ​​of array $array9 are:

Array ( [1] => a [2] => b [one] = > c [two] => d [3] => e [4] => f [5] => g )

4>Variables can also be used as keys of arrays, as shown below:
Code As follows:

1 <?php 
2 $key1=&#39;one&#39;; 
3 $key2=&#39;two&#39;; 
4 $key3=&#39;three&#39;; 
5 $array10[$key1]=&#39;a&#39;; 
6 $array10[$key2]=&#39;b&#39;; 
7 $array10[$key3]=&#39;c&#39;; 
8 echo &#39;<p>数组$array10的键和值为:</p>&#39;; 
9 print_r($array10); 
10 ?>

The result is:

The keys and values ​​of array $array10 are:

Array ( [one] => a [two] => b [three] => c )

two , How to access the elements of the array

1. General method

To get an element in the array, just use the array name plus square brackets and a key. The calling method is as follows:

$arrayname[key ];

2. Use foreach results to traverse the array

If you want to access each array element, you can use a foreach loop:

Foreach ($array as $value)

{

//Do something with $value

}

Foreach loop will iterate each element in the array $array and assign the value of each element to the $value variable. Here is an example:
The code is as follows:

1 <?php 
2 $array11=array(&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;); 
3 echo &#39;<p>数组$array11的值为:&#39;; 
4 foreach($array11 as $value) 
5 { 
6 echo $value.&#39;,&#39;; 
7 } 
8 echo &#39;</p>&#39;; 
9 ?>

The output result is:

array$ The values ​​of array11 are: a,b,c,d,e,

使用foreach还可以同时访问数组元素的键和值,可以使用: 

Foreach($array as $key => $value) 



//Do something with $key and $value 



其中$key为每个元素的键,$value元素的值,下面的代码演示如何使用foreach结构创建一个下拉框: 
代码如下: 

1 <?php 
2 $array12=array(&#39;one&#39;=>1,&#39;two&#39;=>2,&#39;three&#39;=>3,&#39;four&#39;=>4,&#39;five&#39;=>5); 
3 echo &#39;<select name="onetofive">&#39;; 
4 foreach($array12 as $key => $value) 
5 { 
6 echo "<option value=\"$value\">$key</option>"; 
7 } 
8 echo &#39;</select>&#39;; 
9 ?>


3、 使用list函数访问数组 

List函数是把数组中的值赋给一些变量,其函数语法如下: 

Void list(mixed varname, mixed varname2……) 

看如下示例: 
 代码如下: 

1 <?php 
2 $array13=array(&#39;red&#39;,&#39;blue&#39;,&#39;green&#39;); 
3 //赋值给所有的变量 
4 list($flag1,$sky1,$grassland1)=$array13; 
5 echo "$flag1 $sky1 $grassland1"; 
6 echo &#39;<br>&#39;; 
7 //赋值给部分变量 
8 list($flag2,,$grassland2)=$array13; 
9 echo "$flag2 $grassland2"; 
10 echo &#39;<br>&#39;; 
11 //只赋值给第三个变量 
12 list(,,$grassland3)=$array13; 
13 echo "$grassland3"; 
14 echo &#39;<br>&#39;; 
15 ?>

输出结果为: 

red blue green 
red green 
green 

注意: list() 仅能用于数字索引的数组并且数字索引必须从 0 开始。 

因为list函数是先把数组中键为0的元素值赋值给第一个变量,再把键为1的元素值赋值给第二个变量,以此类推,所以list函数中的变量个数和位置必须和数组中的数字键相对应,才能获得想要的值,而且list函数是访问不到以字符串作为键的数组元素的。如下所示: 
 代码如下: 

1 <?php 
2 $array13=array(1=>&#39;red&#39;,&#39;blue&#39;,&#39;green&#39;); 
3 list($flag1,$sky1,$grassland1)=$array13; 
4 echo &#39;$flag1的值为:&#39;.$flag1.&#39;<br>&#39;; 
5 echo &#39;$sky1的值为:&#39;.$sky1.&#39;<br>&#39;; 
6 echo &#39;$grassland1的值为:&#39;.$grassland1.&#39;<br>&#39;; 
7 ?>

其输出结果为: 

$flag1的值为: 
$sky1的值为:red 
$grassland1的值为:blue 

说明:因为$flag1的值本应为数组中键为0的元素值,但此数组首元素是以1为键,没有键为0的元素,所以$flag1的值为空,因此也导致后面$sky1和$grassland1的值发生了变化。

4、 使用each函数访问数组 

each 函数是返回数组中当前的键/值对并将数组指针向前移动一步,注意是一对,下面详细说明。该函数语法: 

array each ( array &$array ) 

返回 array 数组中当前指针位置的键/值对并向前移动数组指针。每一个键值对被返回为四个单元的数组,键值为 0,1,key 和 value四个元素。元素 0 和 key 包含有数组单元的键名,1 和 value 包含有数据。如果内部指针越过了数组的末端,则 each() 返回 FALSE。这里面为什么each函数有四个下表呢?其实each函数得到这四个下标只是方便我们操作而已,我们可以用0,1作为索引,也可以用key,value作为索引。请看下列代码: 
代码如下: 

1 <?php 
2 $arr=array("我是第一个值","我是第二个值","我是第三个值"); 
3 echo "当我们用0,1为索引时:<br/><br/>"; 
4 $a=each($arr); 
5 echo "我在\$arr数组中的键为:".$a[&#39;0&#39;]; 
6 echo "<br/>"; 
7 echo "我在\$arr数组中的值为:".$a[&#39;1&#39;]; 
8 echo "<br/><br/>"; 
9 echo "当我们用key,value为索引时:<br/><br/>"; 
10 $b=each($arr); 
11 echo "我在\$arr数组中的键为:".$b[&#39;key&#39;]; 
12 echo "<br/>"; 
13 echo "我在\$arr数组中的值为:".$b[&#39;value&#39;]; 
14 ?>

显示为: 

当我们用0,1为索引时: 
我在$arr数组中的键为:0 
我在$arr数组中的值为:我是第一个值 
当我们用key,value为索引时: 

我在$arr数组中的键为:1 
我在$arr数组中的值为:我是第二个值 

5、 用each函数与list函数结合来遍历数组,如下例: 
代码如下: 

1 <?php 
2 $array14=array(&#39;a&#39; => &#39;apple&#39;, &#39;b&#39; => &#39;banana&#39;, &#39;c&#39; => &#39;cranberry&#39;); 
3 while(list($key,$value) = each($array14)) 
4 { 
5 echo "$key => $value\n"; 
6 } 
7 ?>

其输出结果为: 

a => apple b => banana c => cranberry 

6、使用for循环访问数组 

如下例所示: 
 代码如下: 

1 <?php 
2 $array15=array(&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;,&#39;f&#39;); 
3 for($i=0;$i<count($array15);$i++) 
4 { 
5 echo &#39;数组元素:&#39;.$array15[$i].&#39;<br>&#39;; 
6 } 
7 ?>

输出结果为: 

数组元素:a 
数组元素:b 
数组元素:c 
数组元素:d 
数组元素:e 
数组元素:f 

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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment