Summary of basic knowledge of PHP array functions

墨辰丷
Release: 2023-03-29 14:22:01
Original
1260 people have browsed it

This article mainly summarizes the most basic knowledge points of PHP array functions. Interested friends can refer to it

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 - contains a Or an array of multiple arrays

1. Create an array

array(key => value)

1. Create an index array

Use the array() function to declare an array. PHP is a weakly typed language that is relatively flexible and convenient. It can also be the element value of the 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] = "腾讯";
Copy after login

2. Create an associative array

Associative arrays are similar to index arrays, except that they are associated Arrays cannot only be numbers like the key names of index arrays. They can be numbers, 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

<?php
$arr = array("百度","阿里","腾讯");
echo count($arr);
?> //结果返回3(说明数组中有三个元素)
Copy after login

2. Traverse the array

Output the values ​​of the elements in the 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) loop, as follows:

Use for loop

<?php
$arr = array("百度","阿里","腾讯");
$arrlen = count($arr);//获取数组的长度
for ($i=0; $i <$arrlen ; $i++) { 
  $data[] = $arr[$i]; 
}
echo "<pre class="brush:php;toolbar:false">"; //换行显示
print_r($data);
Copy after login

The print result is displayed as follows:
Array
(
[0] => Baidu
[1] => Ali
[2] => Tencent
)
Use foreach loop

<?php
$arr = array("百度","阿里","腾讯");
foreach ($arr as $value) {
  $data[] = $value;
}
echo "<pre class="brush:php;toolbar:false">"; //换行显示
print_r($data);//打印结果和上面一样
Copy after login

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:

<?php
$arr = array("李彦宏" => "百度","马云" => "阿里","马化腾" => "腾讯");
foreach ($arr as $key => $value) {
  $data[$key] = $value;
}
echo "<pre class="brush:php;toolbar:false">"; //换行显示
print_r($data);
Copy after login

The printed result shows:

Array
(
  [李彦宏] => 百度
  [马云] => 阿里
  [马化腾] => 腾讯
)
Copy after login

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

Increase ## at 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 then returns the length of the new array.
This function is equivalent to calling array[]=value multiple times.

<?php
$arr = array("百度","阿里","腾讯");
array_push($arr,"知乎","微博");
echo "<pre class="brush:php;toolbar:false">"; //换行显示
print_r($arr);
//打印结果显示:
Array
(
  [0] => 百度
  [1] => 阿里
  [2] => 腾讯
  [3] => 知乎
  [4] => 微博
)
Copy after login

Increase 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.

<?php
$arr = array("百度","阿里","腾讯");
array_unshift($arr,"知乎","微博");
echo "<pre class="brush:php;toolbar:false">"; //换行显示
print_r($arr);
//打印结果显示:
Array
(
  [0] => 知乎
  [1] => 微博
  [2] => 百度
  [3] => 阿里
  [4] => 腾讯
)
Copy after login

Delete at the end of the array element

array_pop(array) function deletes the last element in the array.

<?php
$arr = array("百度","阿里","腾讯");
array_pop($arr);
echo "<pre class="brush:php;toolbar:false">"; //换行显示
print_r($arr);
打印结果显示:
Array
(
  [0] => 百度
  [1] => 阿里
)
Copy after login

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.

<?php
$arr = array("百度","阿里","腾讯");
array_shift($arr);
echo "<pre class="brush:php;toolbar:false">"; //换行显示
print_r($arr);
打印结果显示:
Array
(
  [0] => 阿里
  [1] => 腾讯
)
Copy after login

Remove duplicate values ​​from the array

array_unique(array) function removes duplicate values ​​from the array and returns the result array.

<?php
$arr = array("百度","阿里","腾讯","百度","微博");
$data = array_unique($arr);
echo "<pre class="brush:php;toolbar:false">";
print_r($data);
打印结果显示:
Array
(
  [0] => 百度
  [1] => 阿里
  [2] => 腾讯
  [4] => 微博
)
Copy after login

4. Locate array elements

Search for values ​​existing 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.

<?php
$arr = array("百度","阿里","腾讯");
while (in_array("百度", $arr)) {
  echo "已经找到";
  break;
} //输出已经找到
Copy after login

Remove 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 可选。数值。规定被返回数组的长度。
如果该值设置为整数,则返回该数量的元素。
如果该值设置为负数,则函数将在举例数组末端这么远的地方终止取出。
如果该值未设置,则返回从 start 参数设置的位置开始直到数组末端的所有元素。

<?php
$arr = array("百度","阿里","腾讯","知乎","微博");
$data = array_slice($arr,0,4);
echo "<pre class="brush:php;toolbar:false">";
print_r($data);
打印结果显示:
Array
(
  [0] => 百度
  [1] => 阿里
  [2] => 腾讯
  [3] => 知乎
)
Copy after login

array_splice(array,start,length,array) 函数从数组中移除选定的元素,并用新元素取代它。该函数也将返回包含被移除元素的数组。

<?php
$arr1 = array("百度","阿里","腾讯");
$arr2 = array("知乎","微博");
array_splice($arr1,0,2,$arr2);
echo "<pre class="brush:php;toolbar:false">";
print_r($arr1);
打印结果显示:
Array
(
  [0] => 知乎
  [1] => 微博
  [2] => 腾讯
)
Copy after login

五、数组合并、拆分、比较

array_merge()函数将数组合并到一起,返回一个联合的数组。所得到的数组以第一个输入数组参数开始,按后面数组参数出现的顺序依次追加。

<?php
$arr1 = array("百度","阿里","腾讯");
$arr2 = array("知乎","微博");
$data = array_merge($arr1,$arr2);
echo "<pre class="brush:php;toolbar:false">";
print_r($data);
打印结果显示:
Array
(
  [0] => 百度
  [1] => 阿里
  [2] => 腾讯
  [3] => 知乎
  [4] => 微博
)
Copy after login

递归追加数组
array_merge_recursive()函数与array_merge()相同,可以将两个或多个数组合并到一起,形成一个联合的数组。两者之间的区别在于,当某个输入数组中的某个键已经存在于结果数组中时该函数会采取不同的处理方法。array_merge()会覆盖前面存在的键/值对,将其替换为当前输入数组中的键/值对,而array_merge_recursive()将两个值合并在一起,形成一个新的数组并以原有的键作为数组名。其形式为:

$arr= array(&#39;one&#39;=>&#39;C&#39;, &#39;one&#39;=>&#39;B&#39;); 
$arr1= array(&#39;three&#39;=>&#39;1&#39;, &#39;one&#39;=>&#39;2&#39;); 
$arr2= array_merge_recursive($arr, $arr1); 
echo "<pre class="brush:php;toolbar:false">";
print_r($arr2); 
打印结果显示:
Array
(
  [one] => Array
    (
      [0] => B
      [1] => 2
    )

  [three] => 1
)
Copy after login

合并两个数组
array_combine()函数会生成一个新数组,这个数组由一组提交的键和对应的值组成,其形式为:

$arr= array(&#39;A&#39;, &#39;B&#39;); 
$arr1= array(&#39;1&#39;, &#39;2&#39;); 
$arr2= array_combine($arr, $arr1);
echo "<pre class="brush:php;toolbar:false">";
print_r($arr2); 
打印结果显示:
Array
(
  [A] => 1
  [B] => 2
)
Copy after login

求数组的交集
array_intersect()函数返回一个保留了键的数组,这个数组只由第一个数组中出现的且在其他每个输入数组中都出现的值组成。其形式如下:

$arr= array(&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;); 
$arr1= array(&#39;A&#39;, &#39;B&#39;, &#39;E&#39;); 
$arr2= array(&#39;A&#39;, &#39;F&#39;, &#39;D&#39;); 
$arr3= array_intersect($arr, $arr1, $arr2); 
echo "<pre class="brush:php;toolbar:false">";
print_r($arr3); 
打印结果显示:
Array
(
  [0] => A
)
Copy after login

注意:只有在两个元素有相同的数据类型时,array_intersect()才会认为它们相等。

求关联数组的交集
array_intersect_assoc()与array_intersect()基本相同,只不过它在比较中还考虑了数组的键。因此,只有在第一个数组中出现,且在所有其他输入数组中也出现的键/值对才被返回到结果数组中。其形式如下:

$arr= array(&#39;a&#39;=>&#39;A&#39;, &#39;b&#39;=>&#39;B&#39;, &#39;c&#39;=>&#39;C&#39;, &#39;d&#39;=>&#39;D&#39;); 
$arr1= array(&#39;a&#39;=>&#39;A&#39;, &#39;c&#39;=>&#39;B&#39;, &#39;E&#39;); 
$arr2= array(&#39;a&#39;=>&#39;A&#39;, &#39;b&#39;=>&#39;F&#39;, &#39;d&#39;=>&#39;B&#39;); 
$arr3= array_intersect_assoc($arr, $arr1, $arr2); 
echo "<pre class="brush:php;toolbar:false">";
print_r($arr3); 
打印结果显示:
Array
(
  [a] => A
)
Copy after login

求关联数组的差集
函数array_diff_assoc()与array_diff()基本相同,只是它在比较时还考虑了数组的键,因此,只在第一个数组中出现而不在其他输入数组中出现的键/值对才会被返回到结果数组中。其形式如下:

$arr= array(&#39;a&#39;=>&#39;A&#39;, &#39;b&#39;=>&#39;B&#39;, &#39;c&#39;=>&#39;C&#39;, &#39;d&#39;=>&#39;D&#39;); 
$arr1= array(&#39;a&#39;=>&#39;A&#39;, &#39;b&#39;=>&#39;B&#39;, &#39;e&#39;=>&#39;E&#39;); 
$arr3= array_diff_assoc($arr, $arr1); 
echo "<pre class="brush:php;toolbar:false">";
print_r($arr3); 
打印结果显示:
Array
(
  [c] => C
  [d] => D
)
Copy after login

其他有用的数组函数
返回一组随机的键 array_rand()函数将返回数组中的一个或多个键。其形式为:

$arr= array(&#39;a&#39;=>&#39;A&#39;, &#39;b&#39;=>&#39;B&#39;, &#39;c&#39;=>&#39;C&#39;, &#39;d&#39;=>&#39;D&#39;); 
$arr1= array_rand($arr, 2); 
echo "<pre class="brush:php;toolbar:false">";
print_r($arr1);
打印结果显示:
 Array
(
  [0] => c
  [1] => d
) //每次刷新显示的结果都不一样
Copy after login

对数组中的值求和
array_sum()函数将数组内的所有值加在一起,返回最终的和,其形式如下:

$arr= array(&#39;A&#39;, 32, 12, &#39;B&#39;); 
$count= array_sum($arr); 
echo "<pre class="brush:php;toolbar:false">";
print_r($count);
Copy after login

打印结果显示:
44

如果数组中包含其他数据类型(例如字符串),这些值将被忽略。

划分数组
array_chunk()函数将数组分解为一个多维数组,这个多维数组由多个包含size个元素的数组所组成。其形式如下:

$arr= array(&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;); 
$arr1= array_chunk($arr, 2); 
echo "<pre class="brush:php;toolbar:false">";
print_r($arr1);
Copy after login

打印结果显示:

Array
(
  [0] => Array
    (
      [0] => A
      [1] => B
    )

  [1] => Array
    (
      [0] => C
      [1] => D
    )

)
Copy after login

处理数组时可调用函数有

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*...)
……
Copy after login

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

php中实现进程锁与多进程的方法

PHP+MariaDB数据库操作基本技巧备忘总结

php实现操纵mysqli数据库的方法

The above is the detailed content of Summary of basic knowledge of PHP array functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!