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 " "; <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 "</p>"; $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 "
?>
for loop traversal
The code is as follows | Copy code | ||||
$speed = range(0,220,20); for($i =0;$i
|
The code is as follows | Copy code |
$speed = range(0,220,20);
echo current($speed);//Output the value of the current position (at the beginning of the array)
$i = rand(1,11);
while($i--){
next($speed);//Move the pointer one position backward from the current position
}
echo current($speed);//Output the value of the current position
echo " "; 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 |
$speed = range(0,200,40);
echo "each moves the pointer down "; 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 "数组中存在";
?> |
代码如下 | 复制代码 | |||
$staff = array( array("姓名","性别","年龄"), array("小张","男",24), array("小王","女",25), array("小李","男",23) ); echo "
?> |
代码如下 | 复制代码 |
$array = range(0,9); $num = rand(0,8); while($num--) next($array); $key = key($array); echo $key; ?> |
代码如下 | 复制代码 | |||
$staff = array(
array("姓名","性别","年龄"),
array("小张","男",24),
array("小王","女",25),
array("小李","男",23)
);
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"; } ?> |
代码如下 | 复制代码 |
$array = array("b","c","d","a");
sort($array);//从低到高排序
print_r($array);
echo " "; 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 )代码如下 | 复制代码 |
array = array ('A', 'B', 'C' ); $array = array ('A', 'B', 'C' ); $array = array ('A', 'B', 'C' ); $array = array ('A', 'B', 'C' ); |
The code is as follows | Copy code |
$array = array(1=>"sort2",4=>"Sort5",2=>"sort1",3=>"sort4"); ksort($array);//Sort from low to high print_r($array); ?> |
The code is as follows | Copy code |
array = array ('A', 'B', 'C' ); //Use int array_unshift(array $array,mixed variable[,mixed variable...]) to add elements to the head of the array array_unshift ( $array, 'E', 'F', 'G' ); var_dump ( $array ); $array = array ('A', 'B', 'C' ); //Use int array_push(array $array,mixed variable[,mixed variable...]) to add elements at the end of the array array_push ( $array, 'E', 'F', 'G' ); var_dump ( $array ); $array = array ('A', 'B', 'C' ); //Use mixed array_shift(array $array) to delete elements at the head of the array array_shift ( $array ); var_dump ( $array ); $array = array ('A', 'B', 'C' ); //Use mixed array_pop(array $array) to delete elements at the end of the array array_pop ( $array ); var_dump ( $array ); |
/*
* Search for a specific value in the array, return TRUE if found otherwise return FALSE
* boolean in_array(mixed needle,array haystack[,boolean strict])
* Find a specified key in the array, return TRUE if found, otherwise return FALSE
* boolean array_eky_exists(mixed key,array array)
* Search for a specific value in the array, return TRUE if found otherwise return FALSE
* boolean array_search(mixed needle,array haystack[,boolean strict])
* Get a new array composed of all keys of the array
* array array_keys(array array[,mixed search_value])
* Get a new array composed of all values in the array
* array array_values(array array)
* Determine array size
* integer count(array array[,int mode])
* integer sizeof(array array[,int mode])
* Count the frequency of occurrence of array elements
* array array_count_values(array array)
* Remove duplicate values from the array and return an array composed of unique values
* array array_unique(array array)
* Reverse the order of array elements. If preserve_key is TRUE, the order of array key values will remain unchanged
* array array_reverse(array array[,boolean preserve_key])
* Replace array keys and values
* array array_flip(array array)
* Array order sorting, sort_flags parameter is optional, default behavior
* SORT_NUMBERIC, sort by numerical value, useful for sorting integers or floating point numbers
*SORT_REGULAR, sort by ASCII value
* SORT_STRING, sorted in the correct order known by people closest to you
* The key value order of the asort function remains unchanged
* void sort(array array[,int sort_flags])
* void asort(array array[,int sort_flags])
* Sort the array in reverse order, the sort_flags parameter is optional, and the default behavior is
* SORT_NUMBERIC, sort by numerical value, useful for sorting integers or floating point numbers
*SORT_REGULAR, sort by ASCII value
* SORT_STRING, sorted in the correct order known by people closest to you
* The key value order of the arsort function remains unchanged
* void rsort(array array[,int sort_flags])
* void arsort(array array[,int sort_flags])
* Natural sorting of arrays
* void natsort(array array)
* Case-insensitive natural sorting
* void natcasesort(array array)
* Sort array by key value
* boolean ksort(array array[,int sort_flags])
* Sort the array in reverse order by key value
* boolean krsort(array array[,int sort_flags])
* Sort according to user-defined order
* void usort(array array,callback function_name)
* Merge the arrays together to return a combined array. The back of array_merge covers the front, array_merge_recursive merges together
* array array_merge(array array1[array array2...])//More than one
* array array_merge_recursive(array array1,array array2[,array…])//More than two
* Keys and values form a new array
* array array_combine(array key,array value)
* Return a part of the array, starting from offset and ending at offset+length
* array array_slice(array array, int offset [,int length])
* Delete all elements starting from offset and ending at offset+length, and return the deleted elements in the form of an array
* array array_splice(array, int offset [,int length[,array peplacement]])
* Find the intersection of arrays, the key value is the key value in the first array
* array array_intersect(array array1,array array2[,arrayN……])
* Find the intersection of arrays that contains equal key values, and the key value is the key value in the first array
* array array_intersect_assoc(array array1,array array2[,arrayN……])
* Find the difference set of arrays, the first array has values that are not found in other arrays
* array array_diff(array array1,array array2[,arrayN……])
* Find the difference set of arrays. The first array contains equal key values in values that are not found in other arrays
* array array_diffassoc(array array1,array array2[,arrayN……])
* Return one or more key values in the array
* mixed array_rand(array array[,int num_entries])
* Instant shuffle function
* void shuffle(array input_array)
* Sum the values in the array
* mixed array_sum(array array);
* Decompose the array into a multi-dimensional array, which contains size elements
* array array_chunk(array array, int size [,boolean preserve_keys])
*/

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

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

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

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

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

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

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

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools