This article shares the basic knowledge of PHP array functions for your reference. The specific content is as follows
Array array is a very important data type. Compared to other data types, it is more like a structure, and this structure can store a series of values. Arrays can store many values in a single variable name, and a value can be accessed by referencing a subscript.
In PHP, there are three array types:
Indexed array - an array with a numeric index
Associative array - an array with a specified key
Multidimensional array - an array containing one or more arrays
1. Create an array
array(key => value)
1. Create an index array
Use the array() function declaration array. PHP is a weakly typed language that is relatively flexible and convenient. It can also be the element value of an array directly. There is no need for a key value. The index is automatically assigned (the index starts from 0).
Example:
array("1" => "百度","2" => "阿里","3" => "腾讯"); 或者是不使用键值: array("百度","阿里","腾讯"); 当然也可以写成: $arr[0] = "百度"; $arr[1] = "阿里"; $arr[2] = "腾讯";
2. Create an associative array
Associative arrays are similar to index arrays, except that associative arrays cannot only be numbers like the key names of index arrays. It can be For numeric values, strings and mixed forms, the basis for judging whether an array is an associative array is: whether there is a key name in the array that is not a number. No, it's related.
array("Robin Li" => "Baidu","Jack Ma" => "Alibaba","Ma Huateng" => "Tencent");
3. Multidimensional array
array(array(),array()) Two-dimensional array
Get the length of the array-count() function
//结果返回3(说明数组中有三个元素)
2. Traverse the array
Outputs the value of an element in an array. For index arrays, for and foreach are commonly used; for associative arrays, foreach is commonly used. Use the print_r() function to print the results after the loop, or var_dump().
1. Traverse the index array
To traverse and output all values of the index array, you can use for, foreach(array_expression as value), foreach(arrayexpressionaskey => $value) loops, as follows:
Use for loop
"; //换行显示 print_r($data);
The print result is shown as follows:
Array
(
[0] => Baidu
[1] => Ali
[2] => Tencent
)
Use foreach loop
"; //换行显示 print_r($data);//打印结果和上面一样
Note: There is an array symbol [] after data, why?
2. Traverse the associative array
To traverse and output all the values of the associative array, you can use the foreach (array_expression as key=>value) loop, as follows:
"百度","马云" => "阿里","马化腾" => "腾讯"); foreach ($arr as $key => $value) { $data[$key] = $value; } echo ""; //换行显示 print_r($data);Copy after login
Print The result shows:
Array ( [李彦宏] => 百度 [马云] => 阿里 [马化腾] => 腾讯 )
Did you notice? At this time, what is after data is [$key]? Instead of []
A number associative array and a numerical index array,
3. Add and delete elements of the array
Add to the end of the array element
array_push(array,value1 ,value2…) The function adds one or more elements (push) to the end of the array of the first parameter and returns the length of the new array.
This function is equivalent to calling array[]=value multiple times.
"; //换行显示 print_r($arr); //打印结果显示: Array ( [0] => 百度 [1] => 阿里 [2] => 腾讯 [3] => 知乎 [4] => 微博 )
Add at the beginning of the array element
array_unshift(array, value1, value2, value3...) function is used to insert new elements into the array. The values of the new array will be inserted at the beginning of the array.
"; //换行显示 print_r($arr); //打印结果显示: Array ( [0] => 知乎 [1] => 微博 [2] => 百度 [3] => 阿里 [4] => 腾讯 )
Delete at the end of the array element
array_pop(array) function deletes the last element in the array.
"; //换行显示 print_r($arr); 打印结果显示: Array ( [0] => 百度 [1] => 阿里 )
Delete at the beginning of the array element
array_shift(array) function deletes the first element in the array and can return the value of the deleted element.
"; //换行显示 print_r($arr); 打印结果显示: Array ( [0] => 阿里 [1] => 腾讯 )
Remove duplicate values in the array
array_unique(array) function removes duplicate values in the array and returns the result array.
"; print_r($data); 打印结果显示: Array ( [0] => 百度 [1] => 阿里 [2] => 腾讯 [4] => 微博 )
4. Locate array elements
Search for values that exist in the array
in_array(search,array,type) checks whether the specified value exists in the array.
Returns true if the given value search exists in the array array. If the third parameter is set to true, the function returns true only if the element exists in the array and has the same data type as the given value. If the parameter is not found in the array, the function returns false.
Copy after login
Retrieve a value from the array based on conditions: array_slice(array,start,length,preserve)
start is required. numerical value. Specifies the starting position of the element to be retrieved. 0 = first element.
If the value is set to a positive number, it will be taken from front to back.
If the value is set to a negative number, the absolute value of start is taken from back to front. -2 means start from the second to last element of the array.
length Optional. numerical value. Specifies the length of the returned array.
If the value is set to an integer, this number of elements is returned.
If this value is set to a negative number, the function will terminate fetching this far from the end of the example array.
If this value is not set, all elements starting from the position set by the start parameter to the end of the array are returned.
"; print_r($data); 打印结果显示: Array ( [0] => 百度 [1] => 阿里 [2] => 腾讯 [3] => 知乎 )
array_splice(array,start,length,array) function removes the selected element from an array and replaces it with a new element. This function will also return the array containing the removed elements.
"; print_r($arr1); 打印结果显示: Array ( [0] => 知乎 [1] => 微博 [2] => 腾讯 )
5. Array merging, splitting and comparison
array_merge()函数将数组合并到一起,返回一个联合的数组。所得到的数组以第一个输入数组参数开始,按后面数组参数出现的顺序依次追加。
"; print_r($data); 打印结果显示: Array ( [0] => 百度 [1] => 阿里 [2] => 腾讯 [3] => 知乎 [4] => 微博 )
递归追加数组
array_merge_recursive()函数与array_merge()相同,可以将两个或多个数组合并到一起,形成一个联合的数组。两者之间的区别在于,当某个输入数组中的某个键已经存在于结果数组中时该函数会采取不同的处理方法。array_merge()会覆盖前面存在的键/值对,将其替换为当前输入数组中的键/值对,而array_merge_recursive()将两个值合并在一起,形成一个新的数组并以原有的键作为数组名。其形式为:
$arr= array('one'=>'C', 'one'=>'B'); $arr1= array('three'=>'1', 'one'=>'2'); $arr2= array_merge_recursive($arr, $arr1); echo ""; print_r($arr2); 打印结果显示: Array ( [one] => Array ( [0] => B [1] => 2 ) [three] => 1 )Copy after login
合并两个数组
array_combine()函数会生成一个新数组,这个数组由一组提交的键和对应的值组成,其形式为:
$arr= array('A', 'B'); $arr1= array('1', '2'); $arr2= array_combine($arr, $arr1); echo ""; print_r($arr2); 打印结果显示: Array ( [A] => 1 [B] => 2 )Copy after login
求数组的交集
array_intersect()函数返回一个保留了键的数组,这个数组只由第一个数组中出现的且在其他每个输入数组中都出现的值组成。其形式如下:
$arr= array('A', 'B', 'C', 'D'); $arr1= array('A', 'B', 'E'); $arr2= array('A', 'F', 'D'); $arr3= array_intersect($arr, $arr1, $arr2); echo ""; print_r($arr3); 打印结果显示: Array ( [0] => A )Copy after login
注意:只有在两个元素有相同的数据类型时,array_intersect()才会认为它们相等。
求关联数组的交集
array_intersect_assoc()与array_intersect()基本相同,只不过它在比较中还考虑了数组的键。因此,只有在第一个数组中出现,且在所有其他输入数组中也出现的键/值对才被返回到结果数组中。其形式如下:
$arr= array('a'=>'A', 'b'=>'B', 'c'=>'C', 'd'=>'D'); $arr1= array('a'=>'A', 'c'=>'B', 'E'); $arr2= array('a'=>'A', 'b'=>'F', 'd'=>'B'); $arr3= array_intersect_assoc($arr, $arr1, $arr2); echo ""; print_r($arr3); 打印结果显示: Array ( [a] => A )Copy after login
求关联数组的差集
函数array_diff_assoc()与array_diff()基本相同,只是它在比较时还考虑了数组的键,因此,只在第一个数组中出现而不在其他输入数组中出现的键/值对才会被返回到结果数组中。其形式如下:
$arr= array('a'=>'A', 'b'=>'B', 'c'=>'C', 'd'=>'D'); $arr1= array('a'=>'A', 'b'=>'B', 'e'=>'E'); $arr3= array_diff_assoc($arr, $arr1); echo ""; print_r($arr3); 打印结果显示: Array ( [c] => C [d] => D )Copy after login
其他有用的数组函数
返回一组随机的键 array_rand()函数将返回数组中的一个或多个键。其形式为:
$arr= array('a'=>'A', 'b'=>'B', 'c'=>'C', 'd'=>'D'); $arr1= array_rand($arr, 2); echo ""; print_r($arr1); 打印结果显示: Array ( [0] => c [1] => d ) //每次刷新显示的结果都不一样Copy after login
对数组中的值求和
array_sum()函数将数组内的所有值加在一起,返回最终的和,其形式如下:
$arr= array('A', 32, 12, 'B'); $count= array_sum($arr); echo ""; print_r($count);Copy after login
打印结果显示:
44
如果数组中包含其他数据类型(例如字符串),这些值将被忽略。
划分数组
array_chunk()函数将数组分解为一个多维数组,这个多维数组由多个包含size个元素的数组所组成。其形式如下:
$arr= array('A', 'B', 'C', 'D'); $arr1= array_chunk($arr, 2); echo ""; print_r($arr1);Copy after login
打印结果显示:
Array ( [0] => Array ( [0] => A [1] => B ) [1] => Array ( [0] => C [1] => D ) )
处理数组时可调用函数有
array_filter(*array*,*callbackfunction*); array_intersect_uassoc(*array1*,*array2*,*array3*...,*myfunction*) array_intersect_ukey(*array1*,*array2*,*array3*...,*myfunction*) array_reduce(*array*,*myfunction*,*initial*) array_walk(*array*,*myfunction*,*userdata*...) ……
以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。
更多PHP数组函数知识汇总相关文章请关注PHP中文网!