Common php array Functions

Source: Internet
Author: User

 

In the php tutorial, arrays are powerful data types. They can do a lot of things and store different data types in an array. Below we list the common operations of arrays, sort and sort the array by key name.

/* Common functions of Arrays
*
* Array sorting Function
* 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 arrays by element values
* Asort () arsort ()
* 4. Sort arrays by Natural Numbers
* Natsort () // compares uppercase and lowercase letters with natcasescort () // compares uppercase and lowercase letters.
* 5. Sort arrays according to user-defined rules
* Usort () uasort () uksort () sorts keys
* 6. Sort dimension Arrays
* Array_multisort ()
*
* Array functions for splitting, merging, splitting, and joining
* 1. array_slice ()
* 2. array_splice () // Delete
* 3. array_combine () // merge
* 4. array_merge (); // merge
* 5. array_intersect (); // The intersection of multiple arrays
* 6. array_diff (); // returns the difference set of multiple arrays.
*
* Array and data structure functions
* 1. Use an array to implement a stack // advanced post-release
* Array_push () array_pop ()
* 2. Use arrays to implement queues // first-in-first-out
* Array_unshift () array_shift () unset ()
*
*
* Other functions related to Array Operations
* Array_rand ()
* Shuffle ()
* Array_sum ()
* Range ()
*/

// Use simple array sorting
$ Data = array (5, 8, 1, 7, 2 );
Sort ($ data); // sort elements from small to large
Print_r ($ data ); // Array ([0] => 1 [1] => 2 [2] => 5 [3] => 7 [4] => 8)
Rsort ($ data); // sort elements in ascending order.
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 => "two ");
Ksort ($ data_2); // sorts the subscript of the array from small to large.
Print_r ($ data_2 ); // Array ([1] => one [2] => two [5] => five [7] => seven [8] => eight)
Krsort ($ data_2); // sorts the subscript of the array from large to small
Print_r ($ data_2 ); // Array ([8] => eight [7] => seven [5] => five [2] => two [1] => one)

 

// Sort the Array Based on the element value
$ Data_3 = array ("1" => "Linux", "a" => "Apache", "m" => "MySQL ", "l" => "PHP ");
// The difference between asort () arsort and sort () rsort () is that the former keeps the original key name after sorting, while the latter does not keep the original key name, and the key name starts from 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 the array by the "natural number sorting method" (0-9 short priority)
$ 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 sorting Function
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, splitting, and joining
Echo'
';
$ Data_6 = array ("Linux", "Apache", "MySQL", "PHP ");
Print_r (array_slice ($ data_6,); // remove the Elements marked as 1 and 2
// Array ([0] => Apache [1] => MySQL) subscript reset starts from 0
Echo'
';

Print_r (array_slice ($ data_6,-2, 1); // get one from the second one, not from 0.
// Array ([0] => MySQL) subscript reset starts from 0
Echo'
';

Print_r (array_slice ($ data_6, 1, 2, true ));
// Array ([1] => Apache [2] => MySQL) Retain the original subscript

Echo'
';


// Array_combine ()
$ A1 = array ("OS", "WebServer", "DataBase", "Language ");
$ A2 = array ("Linux", "Apache", "MySQL", "PHP ");

Print_r (array_combine ($ a1, $ a2); // The first parameter is used as the key name, and the second parameter is used as the value for merging.
// Array ([OS] => Linux [WebServer] => Apache [DataBase] => MySQL [Language] => PHP)

Echo'
';

// Array_merge ()
$ A3 = array ("OS", "WebServer", "DataBase", "Language ");
$ A4 = array ("Linux", "Apache", "MySQL", "PHP ");
$ A5 = $ a3 + $ a4;
Print_r ($ a5); // This is displayed because the subscript of the two arrays is repeated.
// Array ([0] => OS [1] => WebServer [2] => DataBase [3] => Language)
Echo'
';
Print_r (array_merge ($ a3, $ a4); // merge and re-Index
// Array ([0] => OS [1] => WebServer [2] => DataBase [3] => Language [4] => Linux [5] => Apache [6] => MySQL [7] => PHP)

Echo'
';

// Array_intersect ()
$ A7 = array ("OS", "WebServer", "DataBase", "Language", 1, 2, 3 );
$ A8 = array ("Linux", "Apache", "MySQL", "PHP", 2, 3, 4 );
Print_r (array_intersect ($ a7, $ a8); // Array ([5] => 2 [6] => 3)
Echo'
';

// Array_diff ()
$ A9 = array (1, 2, 4 );
$ A10 = array (3, 4, 5, 6 );
Print_r (array_diff ($ a9, $ a10); // Array ([0] => 1 [1] => 2)
// Returns the elements of the first array and the second array.
Echo'
';


// Use arrays to implement stacks
$ B = array (1, 2, 4 );
$ B [] = "a"; // inbound Stack
Array_push ($ B, "B", "c"); // use the function to go to 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 the function to exit the 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 elements in the stack c
Echo'
';

// Use arrays to implement queues
$ C = array (1, 2, 3 );
Print_r ($ c); // Array ([0] => 1 [1] => 2 [2] => 3)
Echo'
';
Array_unshift ($ c, "abc", "bcd"); // enter the team
Print_r ($ c ); // Array ([0] => abc [1] => bcd [2] => 1 [3] => 2 [4] => 3)
Echo'
';
$ Values = array_shift ($ c); // leaves
Print_r ($ c); // Array ([0] => bcd [1] => 1 [2] => 2 [3] => 3)
Echo'
';
Unset ($ c [2]); // deletes the specified position element.
Print_r ($ c); // Array ([0] => bcd [1] => 1 [3] => 3)
Echo'
';


// Array_rand () returns the array subscript randomly.
$ Arr = array );
Echo array_rand ($ arr); // returns the subscript of a random array element.
Echo $ arr [array_rand ($ arr)]; // randomly display the values of array elements
Echo'
';
// Shuffle () randomly rearrange the Array
$ Arr2 = array (32, 35, 33 );
Shuffle ($ arr2 );
Print_r ($ arr2); // random location transformation of array elements
Echo'
';
// Array_sum () summation
$ Arr3 = array (1, 3, 5 );
Echo array_sum ($ arr3); // return 9
Echo'
';
Print_r ($ arr3); // Array ([0] => 1 [1] => 3 [2] => 5)
Echo'
';
// Range (minimum value, maximum value, step size)
$ Arr4 = range (0,100, 10 );
Print_r ($ arr4 ); // Array ([0] => 0 [1] => 10 [2] => 20 [3] => 30 [4] => 40 [5] => 50 [6] => 60 [7] => 70 [8] => 80 [9] => 90 [10] => 100)

?>


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.