PHP array operation function study notes_PHP tutorial
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(
); |
2. Create 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 "
$keys = array("string","2",9,"SDK","PK"); $array2 = array_fill_keys($keys,"testing"); 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 "
$keys = array("string","2",9,"SDK","PK"); $array2 = array_fill_keys($keys,"testing"); 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 ""; while(list($keys,$value) = each($staff)){ list($name,$sex,$age) = $value; echo ""; } 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 "
while(list($keys,$value) = each($staff)){
list($name,$sex,$age) = $value; 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 "";
?>
for loop traversal
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 "
$result = array_merge_recursive($array1,$array2,$array3,$array4,$array5); 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 "
|
运行结果:
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 ( [0] => red [1] => blue [2] => yellow [3] => Black ) Array ( [red] => 0 [blue] => 1 [yellow] => 2 [Black] => 3 )
代码如下 | 复制代码 |
$array = array("red","blue","yellow","Black"); |
代码如下 | 复制代码 | ||||
$array = array("red","blue","yellow","Black"); ";
print_r($result); |
运行结果:
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 )
Array ( [0] => red [1] => blue [2] => yellow [3] => Black ) 实例六:array_search()搜索数值:
代码如下 | 复制代码 |
$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 )
/* 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
![]() Hot AI Tools![]() Undresser.AI UndressAI-powered app for creating realistic nude photos ![]() AI Clothes RemoverOnline AI tool for removing clothes from photos. ![]() Undress AI ToolUndress images for free ![]() Clothoff.ioAI clothes remover ![]() AI Hentai GeneratorGenerate AI Hentai for free. ![]() Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Assassin's Creed Shadows: Seashell Riddle Solution
2 weeks ago
By DDD
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
![]() Hot Tools![]() Notepad++7.3.1Easy-to-use and free code editor ![]() SublimeText3 Chinese versionChinese version, very easy to use ![]() Zend Studio 13.0.1Powerful PHP integrated development environment ![]() Dreamweaver CS6Visual web development tools ![]() SublimeText3 Mac versionGod-level code editing software (SublimeText3) ![]() Hot Topics
CakePHP Tutorial
![]() ![]() ![]() PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati ![]() CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu ![]() Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c ![]() CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks. ![]() This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an ![]() JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably, ![]() A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total ![]() If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op ![]() |