Common functions of PHP arrays _php Tutorial

Source: Internet
Author: User
Tags shuffle
In the PHP tutorial array is a powerful data type, he can do a lot of things, can store different data types in an array, below we list the array of commonly used operations, sorting, key names, such as the sequence of sorting and other practices.

/* Common functions for arrays
*
* Sorting Functions for arrays
* Sort ()
* Rsort ()
* Usort ()
* Asort ()
* Arsort ()
* Uasort ()
* Ksort ()
* Krsort ()
* Uksort ()
* Uatsort ()
* Natcasesort ()
* Array_multisort ()
*
* 1. Simple array sorting
* Sort () Rsort ()
* 2. Sort the array by key name
* Ksort () Krsort ()
* 3. Sort the array based on the value of the element
* Asort () Arsort ()
* 4. Sort by the "natural number Sort" method
* Natsort ()//case-sensitive letter comparison natcasescort ()//comparison of case-insensitive letters
* 5. Sort the array according to the user custom rule
* Usort () Uasort () Uksort () sort keys
* 6. Sorting of dimension arrays
* Array_multisort ()
*
* Split, Merge, decompose, join array functions
* 1.array_slice ()
* 2.array_splice ()//delete
* 3.array_combine ()//merger
* 4.array_merge ();//merger
* 5.array_intersect ();//intersection of multiple arrays
* 6.array_diff ();//Returns the difference set of multiple arrays
*
* Functions of arrays and data structures
* 1. Using arrays to implement stacks//advanced post-out
* Array_push () Array_pop ()
* 2. Using arrays to implement queues//FIFO
* Array_unshift () Array_shift () unset ()
*
*
* Other functions related to array manipulation
* Array_rand ()
* Shuffle ()
* Array_sum ()
* Range ()
*/

Use of simple array sorting
$data =array (5,8,1,7,2);
Sort ($data);//The elements are sorted from small to large
Print_r ($data);//array ([0] = 1 [1] = 2 [2] = 5 [3] = 7 [4] = 8)
Rsort ($data);//The elements are sorted from large to small
Print_r ($data);//array ([0] = 8 [1] = 7 [2] = 5 [3] = 2 [4] = 1)

Example of sorting by key name
$data _2=array (5=> "Five",8=> "eight",1=> "one",7=> "seven",2=> "one");
Ksort ($data _2);//The subscript of an array is ordered from small to large
Print_r ($data _2);//array ([1] = one [2] = [5] = = five [7] = = Seven [8] = eight)
Krsort ($data _2);//The subscript of an array is sorted from large to small
Print_r ($data _2);//array ([8] = eight [7] = seven [5] = = Five [2] = [1] = one)

//Sort the array according to the value of the element
$data _3=array ("1" = "Linux", "a" and "Apache", "M" = "MySQL", "l" = "PHP");
The difference between the Asort () Arsort and the sort () Rsort () is that the former is sorted and retains the original key name, which does not retain the original key name, and the key name starts at 0
Asort ($data _3);
Print_r ($data _3);//array ([A] = Apache [1] = = Linux [m] + MySQL [l] + PHP)
Echo '
';
Arsort ($data _3);
Print_r ($data _3);//array ([l] + PHP [m] + MySQL [1] = = Linux [a] = Apache)
Echo '
';
Sort ($data _3);
Print_r ($data _3);//array ([0] = Apache [1] = Linux [2] = + MySQL [3] = PHP)
Echo '
';
Rsort ($data _3);
Print_r ($data _3);//array ([0] = PHP [1] + MySQL [2] = = Linux [3] = Apache)

Sort by "natural number sorting" (0-9 short preferred)
$data _4=array ("file.txt", "File11.txt", "File2.txt", "file22.txt");
Sort ($data _4);
Print_r ($data _4);//array ([0] = file.txt [1] = file11.txt [2] = = File2.txt [3] = = File22.txt)
Echo '
';
Natsort ($data _4);
Print_r ($data _4);//array ([0] = file.txt [2] = file2.txt [1] = = File11.txt [3] = = File22.txt)
Echo '
';
Natcasesort ($data _4);
Print_r ($data _4);//array ([0] = file.txt [2] = file2.txt [1] = = File11.txt [3] = = File22.txt)
Echo '
';

User-defined Sort functions
Echo '
';
$data _5=array ("Linux", "Apache", "MySQL", "PHP");
Usort ($data _5, "Sortbylen");//Sort by element length
Print_r ($data _5);//array ([0] = + PHP [1] = MySQL [2] = = Linux [3] = Apache)
function Sortbylen ($one, $two) {
if (strlen ($one) ==strlen ($two))
return 0;
Else
Return (strlen ($one) >strlen ($two))? 1:-1;
}

Array functions for splitting, merging, decomposing, joining
Echo '
';
$data _6=array ("Linux", "Apache", "MySQL", "PHP");
Print_r (Array_slice ($data _6,1,2));//Remove the element labeled 1, 2
Array ([0] = Apache [1] = MySQL) subscript Reset starting from 0
Echo '
';

Print_r (Array_slice ($data _6,-2,1));//Return one from the second starting behind, not starting from 0
Array ([0] = MySQL) subscript Reset starting from 0
Echo '
';

Print_r (Array_slice ($data _6,1,2,true));
Array ([1] = Apache [2] = MySQL) Keep the original subscript

Echo '
';


Array_combine ()
$a 1=array ("OS", "WebServer", "DataBase", "Language");
$a 2=array ("Linux", "Apache", "MySQL", "PHP");

Print_r (Array_combine ($a 1, $a 2));//The first parameter as the key name and the second as a value to merge
Array ([OS] = Linux [WebServer] + Apache [DataBase] + MySQL [Language] = PHP)

Echo '
';

Array_merge ()
$a 3=array ("OS", "WebServer", "DataBase", "Language");
$a 4=array ("Linux", "Apache", "MySQL", "PHP");
$a 5= $a $a 4;
Print_r ($a 5);//Because the subscript of two arrays is repeated so this is displayed
Array ([0] = OS [1] = WebServer [2] = = DataBase [3] = Language)
Echo '
';
Print_r (Array_merge ($a 3, $a 4));//merge and re-index
Array ([0] = = OS [1] = WebServer [2] = = [3] = Language [4] = = Linux [5] + Apache [6] => ; MySQL [7] = PHP)

Echo '
';

Array_intersect ()
$a 7=array ("OS", "WebServer", "DataBase", "Language", and "no");
$a 8=array ("Linux", "Apache", "MySQL", "PHP", 2,3,4);
Print_r (Array_intersect ($a 7, $a 8));//array ([5] = 2 [6] = 3)
Echo '
';

Array_diff ()
$a 9=array (1,2,3,4);
$a 10=array (3,4,5,6);
Print_r (Array_diff ($a 9, $a))//array ([0] = 1 [1] = 2)
Returns the first array followed by the second difference element
Echo '
';


Implementing stacks using arrays
$b =array (1,2,3,4);
$b []= "a";//Into the stack
Array_push ($b, "B", "C");//Use the function into the stack
Print_r ($b);//array ([0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = a [5] = b [6] + = c)
Echo '
';

$value =array_pop ($b);//Use function to stack
Print_r ($b);//array ([0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = a [5] = b)
Echo '
';
echo $value;//Display the value of the stack element c
Echo '
';

Using arrays to implement queues
$c =array (a);
Print_r ($c);//array ([0] = 1 [1] = 2 [2] = 3)
Echo '
';
Array_unshift ($c, "abc", "BCD");//Queue
Print_r ($c);//array ([0] = ABC [1] = BCD [2] = 1 [3] = 2 [4] = 3)
Echo '
';
$values =array_shift ($c);//The Team
Print_r ($c);//Array ([0] = BCD [1] = 1 [2] = 2 [3] = 3)
Echo '
';
Unset ($c [2]);//delete the specified position element
Print_r ($c);//array ([0] = BCD [1] = 1 [3] = 3)
Echo '
';


Array_rand () randomly returns the array subscript
$arr =array (1,3,4,5,76,7,99,6,2,3);
echo Array_rand ($arr);//Returns the subscript of a random array element
echo $arr [Array_rand ($arr)];//randomly displays the value of an array element
Echo '
';
Shuffle () randomly rearrange arrays
$arr 2=array (32,35,33);
Shuffle ($arr 2);
Print_r ($arr 2);//array element position random transformation
Echo '
';
Array_sum () sum
$arr 3=array (1,3,5);
Echo array_sum ($arr 3); Returns 9
Echo '
';
Print_r ($arr 3);//array ([0] = 1 [1] = 3 [2] = 5)
Echo '
';
Range (min, max, step)
$arr 4=range (0,100,10);
Print_r ($arr 4);//array ([0] = 0 [1] = [2] = [3] = [4] [5] = [6] = [+] = + [7] =&G T [8] [+] [9] = [100] [ten] + []]

?>


http://www.bkjia.com/PHPjc/445342.html www.bkjia.com true http://www.bkjia.com/PHPjc/445342.html techarticle in PHP tutorial array is a powerful data type, he can do a lot of things, can store different data types in an array, below we list the array of commonly used operations, row ...

  • Related Article

    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.