Home >Backend Development >PHP Tutorial >Personal summary of php array related functions_PHP tutorial
1.array_chunk() splits an array into new array chunks. The number of cells in each array is determined by the size parameter. The last array may have a few fewer elements.
Example
$a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse","d"=>"Cow");
print_r(array_chunk($a,2));
?>
Output:
Array (
[0] => Array ( [0] => Cat [1] => Dog )
[1] => Array ( [0] => Horse [1] => Cow )
)
【
This is very similar to the split tool in Linux.
[root@xen187v tmp]$ cat tmp
1
2
3
4
5
6
7
[root@xen187v tmp]$ split -l 2 tmp
[root@xen187v tmp]$ ls
tmp xaa xab xac xad
[root@xen187v tmp]$ cat xaa
1
2
[root@xen187v tmp]$ cat xab
3
4
[root@xen187v tmp]$ cat xac
5
6
[root@xen187v tmp]$ cat xad
7
】
2.
array_merge() Merges one or more arrays into a single array. [This is a vertical merger]
The array_combine() function creates a new array by merging two arrays, one with keys and the other with keys. [This is a horizontal merger]
If one of the arrays is empty, or the two arrays have different numbers of elements, the function returns false.
Example
$a1=array("a","b","c","d");
$a2=array("Cat","Dog","Horse","Cow");
print_r(array_combine($a1,$a2));
?>
【
This is very similar to the paste command under Linux.
The word paste means to paste. This command is mainly used to merge the contents of multiple files, which is exactly the opposite of the function of the cut command.
When pasting data from two different sources, you need to classify it first and make sure that the two files have the same number of lines
[root@xen187v tmp]$ cat xaa
1
2
[root@xen187v tmp]$ cat xab
3
4
[root@xen187v tmp]$ paste xaa xab
1 3
2 4
Add one more line to xaa and see what happens
[root@xen187v tmp]$ cat xaa
1
2
3
[root@xen187v tmp]$ paste xaa xab
1 3
2 4
3
Let’s see what happens if we add two more lines to xab
[root@xen187v tmp]$ cat xab
i
i
3
4
[root@xen187v tmp]$ paste xaa xab
1 i
2 i
3 3
4
[root@xen187v tmp]$
】
3.
array_sum() calculates the sum of all values in an array.
The array_count_values() function is used to count the number of occurrences of all values in an array.
This function returns an array, the key name of its element is the value of the original array, and the key value is the number of times the value appears in the original array.
[Much like uniq -c
[root@xen187v tmp]$ cat xab
i
i
3
4
[root@xen187v tmp]$ uniq -c xab
2 i
1 3
1 4
[root@xen187v tmp]$ uniq -c xab|awk '{print $2" "$1}'
i 2
3 1
4 1
[root@xen187v tmp]$
】
4.
[Sentiment: It would be great if the names of these array functions were the same as the Linux command names, which would be easier to remember]
5.array_diff() function returns the first array, the array of data items that are not in the subsequent array
6.array_flip() exchanges the keys and values in the array. The function returns a reversed array. If the same value appears multiple times, the last key name will be its value and all other key names will be lost.
If the data type of the value in the original array is not string or integer, the function will report an error.
[This is worth remembering. When processing data, it is easy to encounter key->value flipping situations]
7.array_intersect() calculates the intersection of arrays.
【
A common question in interviews, use native code to find the intersection of two arrays
function intersectArray($arr1,$arr2)
{
$tmpArr = array();
foreach($arr1 as $v1) $tmpArr[$v1] = 0;
foreach($arr2 as $v2)
{
if(isset($tmpArr[$v2])
{
$tmpArr[$v2] = 1;
}
}
//The intersection with a value of 1 in $tmpArr
$retArr = array();
foreach($tmpArr as $key => $v)
{
if($v == 1) $retArr[] = $key;
}
return $retArr;
}
】
8.array_keys() returns all key names in the array.
9.
array_rand() randomly selects one or more elements from an array and returns it.
The shuffle() function rearranges the elements in the array in random order
10.
array_reverse() reverses the order of elements in the original array, creates a new array and returns it.
11.
array_search() searches the array for a given value and returns the corresponding key if successful.
12
array_unique() removes duplicate values from an array.
13
arsort() sorts an array in reverse order and maintains index relationships.
asort() sorts an array and maintains index relationships.
krsort() sorts the array in reverse order by key name.
ksort() sorts the array by key name.