Arrays in PHP are an indispensable part of our application development. Now I will share with you some of my own operations on learning PHP arrays. I hope that students who need to know more can refer to them.
For Web programming, the most important thing is accessing, reading and writing data. There may be many storage methods, including strings, arrays, files, etc. Arrays can be said to be one of the more important methods in PHP data applications. There are many array functions in PHP. The following is a summary of what I learned so that I can learn from them in the future.
. Array definition
The definition of an array is defined using the array() method. You can define an empty array:
The code is as follows | Copy code | ||||||||
$result = array(); $color =array("red","blue","green"); //Custom key value $language = (1=>"English",3=>"Chinese",5=>"Franch");
$two = array(
); |
compact()
compact() function - Converts one or more variables (containing arrays) to an array: array compact (mixed $varname [, mixed $... ]).
The code is as follows | Copy code | ||||
|
The code is as follows | Copy code |
$number = array("1","3","5","7","9"); <🎜> $array = array("I","Am","A","PHP","er"); <🎜> $newArray = array_combine($number,$array); <🎜> print_r ($newArray); <🎜> ?> |
I won’t go into details about the array_combine function, everyone will understand it after reading it.
Running result:
Array ( [1] => I [3] => Am [5] => A [7] => PHP [9] => er )
range()
range() function - creates an array of a specified range:
The code is as follows | Copy code |
代码如下 | 复制代码 |
$array1 = range(0,100,10);//0为起始值,100为结束值,10为步进值(默认步进值为1). print_r($array1); echo" "; $array2 = range("A","Z"); print_r($array2); echo " "; $array3 = range("z","a"); print_r($array3); ?> |
print_r($array1);
echo"
";
代码如下 | 复制代码 |
$array = range(1,10); $fillarray = range("a","d"); $arrayFilled = array_fill(0,5,$fillarray);//这里的$fillarray可以是字符串,如"test". echo " "; <br> print_r ($arrayFilled); <br> echo ""; $keys = array("string","2",9,"SDK","PK"); $array2 = array_fill_keys($keys,"testing"); echo " "; <br> print_r ($array2); <br> echo ""; ?> |
echo "
";
$array3 = range("z","a");
print_r($array3);
?>
array_fill()
array_fill() function - fill array function:
The code is as follows | Copy code |
$array = range(1,10); $fillarray = range("a","d"); $arrayFilled = array_fill(0,5,$fillarray);//$fillarray here can be a string, such as "test". echo " "; <p> print_r ($arrayFilled); <br> echo ""; $keys = array("string","2",9,"SDK","PK"); $array2 = array_fill_keys($keys,"testing"); echo " "; <br> print_r ($array2); <br> echo ""; ?> |
Run result:
Array
(
[0] => Array
(
[0] => a
[1] => b
[3] => d
)
[1] => Array
(
[0] => a
[1] => b
[3] => d
)
[2] => Array
(
[0] => a
[1] => b
[3] => d
)
[3] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => Array
(
[0] => a
[1] => b
[2] => c
代码如下 | 复制代码 |
$speed = array(50,120,180,240,380); |
The code is as follows | Copy code |
$speed = array(50,120,180,240,380); <🎜>
foreach($speed as $keys=>$values){
echo $keys."=>".$values." "; } ?> |
Run result:
0=>50
1=>120
2=>180
3=>240
4=>380
while loop traversal
While loop traversal is generally combined with the list function. The following is an example
The code is as follows | Copy code | |||
代码如下 | 复制代码 | |||
$staff = array( array("姓名","性别","年龄"), array("小张","男",24), array("小王","女",25), array("小李","男",23) ); echo "
?> |
array("name","gender","age"),
array("Xiao Zhang","Male",24),代码如下 | 复制代码 |
$speed = range(0,220,20); for($i =0;$i } ?> |
array("Xiao Li","Male",23)
);
echo "
$name | $sex | $age |
代码如下 | 复制代码 |
$speed = range(0,220,20); echo current($speed);//输出当前位置的值(在数组的开头位置) $i = rand(1,11); while($i--){ next($speed);//指针从当前位置向后移动一位 } echo current($speed);//输出当前位置的值 echo " "; echo prev($speed);//输出前一位置数组值 echo " "; echo reset($speed);//重置数组的指针,将指针指向起始位置 echo " "; echo end($speed);//输出最后位置的数组值 echo " "; ?> |
echo "
The code is as follows | Copy code | ||||
$speed = range(0,220,20); for($i =0;$i 代码如下 |
复制代码 |
$speed = range(0,200,40); |
echo "each实现指针下移 "; echo "0挡的速度是".current(each($speed))." "; echo "1挡的速度是".current(each($speed))." "; echo "2挡的速度是".current(each($speed))." "; echo "3挡的速度是".current(each($speed))." "; echo "4挡的速度是".current(each($speed))." "; echo "5挡的速度是".current(each($speed))." "; echo "使用each函数实现数组指针的移动,进行数组遍历 "; reset($speed);//这里是将数组指针指向数组首 while(list($key,$value)=each($speed)){ echo $key."=>".$value." "; } ?> |
The code is as follows | Copy code |
";
echo prev($speed);//Output the previous position array value
echo " "; echo reset($speed);//Reset the pointer of the array and point the pointer to the starting position echo " "; echo end($speed);//Output the array value of the last position echo " "; ?> |
The code is as follows | Copy code |
";
echo "The speed of 0 gear is".current(each($speed))." "; echo "The speed of 1st gear is".current(each($speed))." "; echo "The speed of 2nd gear is".current(each($speed))." "; echo "The speed of 3rd gear is".current(each($speed))." "; echo "The speed of 4th gear is".current(each($speed))." "; echo "The speed of 5th gear is".current(each($speed))." "; echo "Use each function to move the array pointer and traverse the array "; reset($speed);//This is to point the array pointer to the beginning of the array while(list($key,$value)=each($speed)){ echo $key."=>".$value." "; } ?> |
Run result:
Each implements pointer movement down
The speed of gear 0 is 0
The speed of 1st gear is 40
The speed in 2nd gear is 80
The speed in 3rd gear is 120
The speed in 4th gear is 160
The speed in 5th gear is 200
Use each function to move the array pointer and traverse the array
0=>0
1=>40
2=>80
3=>120
4=>160
5=>200
5. Array addition and deletion operations
Add array members
Example 1: $num[] = value is directly assigned and appended to the end of the array:
[code
The code is as follows | Copy code |
] | |
代码如下 | 复制代码 |
]
$num = array(1=>80,2=>120,3=>160); echo "使用表达式添加数组成员 "; $num[]=240; print_r($num); ?> |
echo "Add array members using expressions
";
$num[]=240;
print_r($num);
?>
代码如下 | 复制代码 |
$num = array(1=>80,2=>120,3=>160); |
Array ( [0] => 80 [1] => 120 [2] => 160 [3] => 240 )
Example 2: array_pad function, selective appending of the beginning and end of an array
The code is as follows | Copy code | ||||||||
$num = array(1=>80,2=>120,3=>160); $num = array_pad($num,4,200); echo "Use the array_pad function to add members to the end of the array ";
echo "
|
The code is as follows | Copy code |
$num = array(1=>80,2=>120,3=>160); array_push($num,200,240,280);//You can append by yourself, directly at the end of the array print_r($num); ?> |
The code is as follows | Copy code |
$num = array(1=>80,2=>120,3=>160); array_unshift($num,0,40);//You can add it yourself, directly at the end of the array print_r($num); ?> |
Run result:
Array ( [0] => 0 [1] => 40 [2] => 80 [3] => 120 [4] => 160 )
Note: After using the array_unshift() function, the key values of the array will start from 0!
Delete array members
Example 1: The unset() command deletes array members or arrays:
The code is as follows | Copy code |
代码如下 | 复制代码 |
$num = array_fill(0,5,rand(1,10)); print_r($num); echo " "; unset($num[4]); print_r($num); echo " "; unset($num); if(is_array){ echo "unset命令不能删除整个数组"; }else{ echo "unset命令可以删除数组"; } ?> |
print_r($num);
echo "
";
unset($num[4]);
echo "
";
unset($num);
if(is_array){
代码如下 | 复制代码 |
$a=array("red", "green", "blue", "yellow"); count ($a); //得到4 array_splice($a,1,1); //删除第二个元素 count ($a); //得到3 echo $a[2]; //得到yellow echo $a[1]; //得到blue ?> |
echo "unset command can delete arrays";
}代码如下 | 复制代码 |
$a=array("red", "green", "blue", "yellow","blue","green"); |
Running result: (Running error and description array are also deleted and no longer exist)
Array ( [0] => 9 [1] => 9 [2] => 9 [3] => 9 [4] => 9 )
Array ( [0] => 9 [1] => 9 [2] => 9 [3] => 9 )
代码如下 | 复制代码 |
$array1 = array("r"=>"red",1,2,3,4); $array2 = array("b"=>"blue",4=>5,6,7,8,9); $array3 = array("r"=>"read",4=>10,2=>11); $array4 = array( array(4=>10), array(7=>13) ); $array5 = array( array(4=>11), array(6=>12) ); $result = array_merge($array1,$array2,$array3,$array4,$array5); echo " "; <br> print_r($result); <br> echo ""; $result = array_merge_recursive($array1,$array2,$array3,$array4,$array5); echo " "; <br> print_r ($result); <br> echo ""; ?> |
The code is as follows | Copy code |
$a=array("red", "green", "blue", "yellow"); <🎜> count ($a); //Get 4 <🎜> array_splice($a,1,1); //Delete the second element <🎜> count ($a); //Get 3 <🎜> echo $a[2]; //get yellow <🎜> echo $a[1]; //get blue <🎜> ?> |
The code is as follows | Copy code |
$a=array("red", "green", "blue", "yellow","blue","green"); <🎜> $result = array_unique($a); <🎜> print_r($result); <🎜> ?> |
The code is as follows | Copy code |
$array1 = array("r"=>"red",1,2,3,4);
$array2 = array("b"=>"blue",4=>5,6,7,8,9);
$array3 = array("r"=>"read",4=>10,2=>11);
$array4 = array(
array(4=>10),
array(7=>13)
);
$array5 = array(
array(4=>11),
array(6=>12)
);
$result = array_merge($array1,$array2,$array3,$array4,$array5);
echo ""; print_r($result); echo ""; $result = array_merge_recursive($array1,$array2,$array3,$array4,$array5); echo " "; print_r ($result); echo ""; ?> |
运行结果:
Array
(
[r] => read
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[b] => blue
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
[10] => 11
[11] => Array
(
[4] => 10
)
[12] => Array
(
[7] => 13
)
[13] => Array
(
[4] => 11
)
[14] => Array
(
[6] => 12
)
)
Array
(
[r] => Array
(
[0] => red
[1] => read
)
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[b] => blue
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
[10] => 11
[11] => Array
(
[4] => 10
)
[12] => Array
(
[7] => 13
)
[13] => Array
(
[4] => 11
)
[14] => Array
(
[6] => 12
)
)
注:1. array_merge的键名是数字的将重新建立索引;遇到相同的字符串键名时,后面的将覆盖前面的。 2. array_merge_recursive函数的作用是将相同字符串的键名单元整合成一个数组。
6. 数组的键值和值操作
实例一:in_array()检测数组中是否有某个值存在
代码如下 | 复制代码 | ||||||||||||||||||||||||||
if(in_array(9,$array)){ echo "数组中存在";
?>
|
实例四:array_flip()交换数组的键值和值:
代码如下 | 复制代码 | ||||||||
print_r($array); echo " "; $array = array_flip($array);
?> |
代码如下 | 复制代码 |
$array = array("red","blue","yellow","Black"); |
代码如下 | 复制代码 | ||||
$array = array("red","blue","yellow","Black"); ";
print_r($result); |
代码如下 | 复制代码 |
$array = array("a","b","c","d"); |
代码如下 | 复制代码 |
$array = array("red","blue","yellow","Black"); <🎜> $result = array_search("red",$array); <🎜> if(($result === NULL)){ <🎜> echo "不存在数值red"; <🎜> }else{ <🎜> echo "存在数值 $result"; <🎜> } <🎜> ?> |
代码如下 | 复制代码 |
"; rsort($array);//逆向排序 print_r($array); ?> |
代码如下 | 复制代码 |
$array = array("a","b","c","d"); <🎜> shuffle($array);//从低到高排序 <🎜> print_r($array); <🎜> ?> |
The result is dynamic:
Array ( [0] => c [1] => a [2] => d [3] => b )
The result of shuffle is a bit random and different every time it is refreshed.
Example 3: array_reverse() array reverse:
The code is as follows | Copy code | ||||||||
$array = array_reverse($array);//Sort from low to high print_r($array); ?>
|
Run result:
Array ( [0] => c [1] => a [2] => b [3] => d )
Example 4: Natural sorting algorithm - natsort() and natcasesort();
The code is as follows | Copy code | ||||||||||||||||
$array = array("sort2","Sort5","sort1","sort4");
print_r($array); ?> Result: Array ( [1] => Sort5 [2] => sort1 [0] => sort2 [3] => sort4 )
/*
Related labels:
source:php.cn
Previous article:PHP uses the key name in the array as the variable name and the key value as the variable_PHP tutorial
Next article:PHP array to xml and xml conversion array example_PHP tutorial
Statement of this Website
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
Latest Articles by Author
Latest Issues
When adding sublime3 to compile system php, use the PHP toolbox, cmd php -v is useless
From 1970-01-01 08:00:00
0
0
0
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|