Phparray-related functions-my summary-PHP Tutorial

Source: Internet
Author: User
Personal summary of phparray functions. 1. array_chunk () splits an array into new array blocks. The number of units in each array is determined by the size parameter. The number of units in the last array may be smaller. Example? Ph 1. array_chunk () splits an array into new array blocks. The number of units in each array is determined by the size parameter. The number of units in the last array may be smaller.
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 () combines one or more numbers into an array. [This is vertical merge]
The array_combine () function combines two arrays to create a new array. one of the arrays is the key name, and the other is the key value. [This is horizontal merge]
If one of the arrays is empty or the number of elements in the two arrays is different, this function returns false.
Example
$ A1 = array ("a", "B", "c", "d ");
$ A2 = array ("Cat", "Dog", "Horse", "Cow ");
Print_r (array_combine ($ a1, $ a2 ));
?>

This is similar to the paste command in linux.
Paste indicates pasting. This command is used to merge the content of multiple files, which is the opposite of the cut command.


When pasting data from two different sources, you must first classify the data and ensure that the number of lines of the two files is the same.
[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 a line to xaa to see what will happen
[Root @ xen187v tmp] $ cat xaa
1
2
3
[Root @ xen187v tmp] $ paste xaa xab
1 3
2 4
3
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 the array.
The array_count_values () function is used to count the number of occurrences of all values in the array.
This function returns an array. The key name of the element is the value of the original array, and the key value is the number of times this value appears in the original array.
[Very similar to 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.


[Emotion: it would be nice to remember these array function names and linux command names]
5. the array_diff () function returns the first array, which is not an array of data items in the subsequent array.
6. array_flip () exchanges keys and values in the array. the function returns an inverted array. if the same value appears multiple times, the last key name will be used as its value, and all other key names will be lost.
If the data type of the value in the original array is not a string or integer, the function reports an error.
[This is worth remembering. when processing data, it is easy to encounter a situation where key-> value needs to be flipped]
7. array_intersect () calculates the intersection of arrays.

Simple questions during the interview, using the native code to calculate 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 value 1 in tmpArr is the intersection.
$ RetArr = array ();
Foreach ($ tmpArr as $ key => $ v)
{
If ($ v = 1) $ retArr [] = $ key;
}
Return $ retArr;
}


]
8. array_keys () returns all the key names in the array.
9.
Array_rand () randomly selects one or more elements from the array and returns them.
The shuffle () function sorts the elements in the array in random order.
10.
Array_reverse () switches the elements in the original array sequentially, creates a new array, and returns the result.
11.
Array_search () searches for a given value in the array. if the value is successful, the corresponding key name is returned.
12
Array_unique () deletes repeated values in the array.
13
Arsort () sorts arrays in reverse order and maintains the index relationship.
Asort () sorts arrays and maintains the index relationship.
Krsort () sorts arrays in reverse order by key name.
Ksort () sorts arrays by key names.

The http://www.bkjia.com/PHPjc/477189.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477189.htmlTechArticle1.array_chunk () splits an array into a new array block. The number of units in each array is determined by the size parameter. The number of units in the last array may be smaller. Example? Ph...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.