PHP array Correlation function Personal Summary _php tutorial

Source: Internet
Author: User
1.array_chunk () splits an array into a new array block. The number of cells in each of these arrays is determined by the size parameter. The number of cells in the last array may be few.
Example
$a =array ("a" = "Cat", "b" = "Dog", "c" = "Horse", "D" and "Cow");
Print_r (Array_chunk ($a, 2));
?>
Output:
Array (
[0] = = Array ([0] = = Cat [1] = Dog)
[1] = = Array ([0] = Horse [1] = Cow)
)

It's very much like 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 an array. "This is a vertical merger."
The Array_combine () function creates a new array by merging two arrays, one of which is the key name and the value of the other array is the key value. "This is a horizontal merger."
If one of the arrays is empty, or if the number of elements in the two array is different, the function returns FALSE.
Example
$a 1=array ("a", "B", "C", "D");
$a 2=array ("Cat", "Dog", "Horse", "Cow");
Print_r (Array_combine ($a 1, $a 2));
?>

This is much like the Paste command under Linux.
Paste word meaning is paste. This command is primarily used to merge the contents of multiple files, just as opposed to the function of the Cut command.


When pasting data from two different sources, you first need to classify them and make sure that two files are the same number of rows
[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 and see what happens.
[Root@xen187v tmp]$ Cat Xaa
1
2
3
[root@xen187v tmp]$ paste Xaa xab
1 3
2 4
3
Give Xab two more lines and see what kind of
[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 () computes the and of all values in the array. The
Array_count_values () function is used to count the occurrences of all values in the array.
This function returns an array whose key name is the value of the original array, and the key value is the number of occurrences of the value 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 $ "" $} '
I 2
3 1
4 1
[root@xen187v tmp]$
"
4.


"It's nice to remember that if these array function names are consistent with the Linux command name, the
5.array_diff () function returns the first array, not the array of data items in the subsequent array
6.array_flip () swaps the keys and values in the array. The function returns an inverted array, and if the same value occurs more than once, the last key name will be the value and all other key names are lost.
If the data type of the value in the original array is not a string or an integer, the function will error.
"This is worth remembering, when processing data, it is easy to encounter key->value to flip"
7.array_intersect () computes the intersection of the array.

An easy question in the interview, use native code to find the intersection of two arrays
function Intersectarray ($arr 1, $arr 2)
{
$tmpArr = array ();
foreach ($arr 1 as $v 1) $TMPARR [$v 1] = 0;
foreach ($arr 2 as $v 2)
{
if (isset ($TMPARR [$v 2])
{
$tmpArr [$v 2] = 1;
}
}
//$TMPARR The value of 1 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 rearranges the elements in the array in random order
10.
Array_reverse () flips the order of elements in the original array, creates a new array, and returns.
11.
Array_search () searches the array for the given value and returns the corresponding key name if successful.
12
Array_unique () deletes duplicate values in the array.
13
Arsort () Reverses the array and maintains the index relationship.
Asort () sorts the array and maintains the index relationship.
The Krsort () array is reversed by key name.
Ksort () The array is sorted by key name.

http://www.bkjia.com/PHPjc/477189.html www.bkjia.com true http://www.bkjia.com/PHPjc/477189.html techarticle 1.array_chunk () splits an array into a new array block. The number of cells in each of these arrays is determined by the size parameter. The number of cells in the last array may be few. 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.