Android programmers learn PHP development (25)-array operation-related functions (3) grouping and arrangement-PhpStorm

Source: Internet
Author: User
These three blog posts demonstrate that almost all commonly used array functions, in PHP development, most of the time is to operate strings and arrays, so related functions are important. These three blog posts demonstrate that almost all commonly used array functions, in php development, most of the time is to operate strings and arrays, so related functions are important.

Http://php.net/manual/zh/ref.array.php

Bubble sorting
Array_slice () extracts a segment from the array
Array_splice () removes a part of the array and replaces it with other values.
Array_combine () creates an array with the value of one array as its key name and the value of the other array as its value

Array addition and array combination
Array_merge () combines one or more arrays

Intersection and difference set
Array_intersect () calculates the intersection of arrays
Array_diff () calculates the difference set of the array

Inbound and outbound stacks
Array_push () pushes one or more units to the end of the array (in the stack)
Array_pop () pops up the last unit of the array (output stack)

Queue
Array_unshift () inserts one or more units (joining) at the beginning of the array)
Array_shift () removes the elements starting with an array

Random
Array_rand () randomly removes one or more units from the array (random)

Disrupt
Shuffle () disrupts the array

Sum
Array_sum () calculates the sum (sum) of all values in the array)

Range () creates an array containing a specified range unit.
Array_fill () fills the array with the given value

 '; $ Arr = array (,); $ tmp; for ($ I = 0; $ I
 
  
$ Arr [$ j + 1]) {$ tmp = $ arr [$ j]; $ arr [$ j] = $ arr [$ j + 1]; $ arr [$ j + 1] = $ tmp ;}} print_r ($ arr ); // Array ([0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9) echo'
  
'; Echo' ---------- bubble sort from large to small ----------
'; $ Arr = array (,); $ tmp; for ($ I = 0; $ I $ Arr [$ j]) {$ tmp = $ arr [$ j]; $ arr [$ j] = $ arr [$ j + 1]; $ arr [$ j + 1] = $ tmp ;}} print_r ($ arr ); // Array ([0] => 9 [1] => 8 [2] => 7 [3] => 6 [4] => 5 [5] => 4 [6] => 3 [7] => 2 [8] => 1 [9] => 0) echo'
';/*** Array_slice () extracts a segment from the array */echo' ---------- array_slice ()----------
'; $ Arr2 = array ("a", "B", "c", "d", "e"); $ arr2n = array_slice ($ arr2, 2, 2); // Extract two elements, print_r ($ arr2n), from subscript 2; // Array ([0] => c [1] => d) echo'
'; $ Arr2n2 = array_slice ($ arr2,-2, 2); // Two elements print_r ($ arr2n2) are extracted starting from (maximum subscript-2 ); // Array ([0] => d [1] => e) echo'
';/*** Array_splice () removes part of the array and replaces */echo' ---------- array_splice () -------- with other values ()----------
'; $ Arr2 = array ("a", "B", "c", "d", "e"); array_splice ($ arr2, 2 ); // remove all the following elements from the subscript 2: print_r ($ arr2); // Array ([0] => a [1] => B) echo'
'; $ Arr2 = array ("a", "B", "c", "d", "e"); array_splice ($ arr2,-3, 2 ); // remove the two elements print_r ($ arr2) from (maximum subscript-3 ); // Array ([0] => a [1] => B [2] => e) echo'
'; $ Arr2 = array ("a", "B", "c", "d", "e"); array_splice ($ arr2,-3, 2, "hello"); // remove two elements from (maximum subscript-3. Add the new element print_r ($ arr2) from the subscript removed ); // Array ([0] => a [1] => B [2] => hello [3] => e) echo'
'; $ Arr2 = array ("a", "B", "c", "d", "e"); array_splice ($ arr2,-3, 2, array ("hello", "world", "android"); // remove two elements from (maximum subscript-3. Add the new element print_r ($ arr2) from the subscript removed ); // Array ([0] => a [1] => B [2] => hello [3] => world [4] => android [5] => e) echo'
';/*** Array_combine () creates an array with the value of one array as its key name and the value of the other array as its value */echo' ---------- array_combine ()----------
'; $ ArrA = array ("OS", "webserver", "db", "language"); $ arrB = array ("linux", "apache ", "mysql", "php"); $ arrC = array_combine ($ arrA, $ arrB); print_r ($ arrC ); // Array ([OS] => linux [webserver] => apache [db] => mysql [language] => php) echo'
';/*** When the array is added, the element of $ arrA overwrites the element of $ arrB */echo' ---------- when the subscript is the same ----------
'; $ ArrA = array ("OS", "webserver", "db", "language"); $ arrB = array ("linux", "apache ", "mysql", "php"); $ arrC = $ arrA + $ arrB; print_r ($ arrC ); // Array ([0] => OS [1] => webserver [2] => db [3] => language) echo'
'; $ ArrA = array ("OS", "webserver", "db", "language"); $ arrB = array (9 => "linux", "apache ", "mysql", "php"); $ arrC = $ arrA + $ arrB; print_r ($ arrC ); // Array ([0] => OS [1] => webserver [2] => db [3] => language [9] => linux [10] => apache [11] => mysql [12] => php) echo'
';/*** Array merge * array_merge () merge one or more arrays * If the subscript is a string, that is, join the array, and the subscript is the same, $ arrB elements overwrite $ arrA elements */echo '---------- array_merge () array merged ----------
'; $ ArrA = array ("OS", "webserver", "db", "language"); $ arrB = array ("linux", "apache ", "mysql", "php"); $ arrC = array_merge ($ arrA, $ arrB); print_r ($ arrC ); // Array ([0] => OS [1] => webserver [2] => db [3] => language [4] => linux [5] => apache [6] => mysql [7] => php) echo'
'; $ ArrA = array ("OS", "two" => "webserver", "db", "language"); $ arrB = array ("linux ", "two" => "apache", "mysql", "php"); $ arrC = array_merge ($ arrA, $ arrB); print_r ($ arrC ); // Array ([0] => OS [two] => apache [1] => db [2] => language [3] => linux [4] => mysql [5] => php) echo'
';/*** Array_intersect () calculates the intersection of arrays */echo' ---------- array_intersect () intersection ----------
'; $ ArrA = array ("OS", "www", "db", "linux"); $ arrB = array ("linux", "apache ", "mysql", "www"); $ arrC = array_intersect ($ arrA, $ arrB); print_r ($ arrC ); // Array ([1] => www [3] => linux) echo'
';/*** Array_diff () calculates the difference set of the array */echo' ---------- array_diff () difference set ----------
'; $ ArrA = array ("OS", "www", "db", "linux"); $ arrB = array ("linux", "apache ", "mysql", "www"); $ arrC = array_diff ($ arrA, $ arrB); print_r ($ arrC ); // Array ([0] => OS [2] => db) echo'
';/*** Array_push () pushes one or more units to the end of the array (in the stack) */echo' ---------- array_push () into the stack ----------
'; $ ArrZhan = array (); array_push ($ arrZhan, "one"); print_r ($ arrZhan); // Array ([0] => one) echo'
'; Array_push ($ arrZhan, "two"); array_push ($ arrZhan, "three"); print_r ($ arrZhan ); // Array ([0] => one [1] => two [2] => three) echo'
'; Array_push ($ arrZhan, "four", "five"); print_r ($ arrZhan ); // Array ([0] => one [1] => two [2] => three [3] => four [4] => five) echo'
';/*** Array_pop () pops up the last unit of the array (Out Stack) */echo' ---------- array_pop () Out Stack ----------
'; Array_pop ($ arrZhan); print_r ($ arrZhan ); // Array ([0] => one [1] => two [2] => three [3] => four) echo'
'; Echo array_pop ($ arrZhan); // four echo'
'; Print_r ($ arrZhan); // Array ([0] => one [1] => two [2] => three) echo'
';/*** Array_unshift () inserts one or more units (into the queue) at the beginning of the array */echo' ---------- array_unshift () queues ----------
'; $ ArrDuilie = array (); array_unshift ($ arrDuilie, "one"); array_unshift ($ arrDuilie, "two"); array_unshift ($ arrDuilie, "three "); array_unshift ($ arrDuilie, "four"); print_r ($ arrDuilie); echo'
'; // Array ([0] => four [1] => three [2] => two [3] => one)/*** array_shift () remove the elements starting with the array from the array (out of the queue) */echo '---------- array_shift () out of the queue ----------
'; Array_shift ($ arrDuilie); print_r ($ arrDuilie); echo'
'; // Array ([0] => three [1] => two [2] => one) echo array_shift ($ arrDuilie); // three echo'
'; Print_r ($ arrDuilie); // Array ([0] => two [1] => one) echo'
';/*** Array_rand () randomly fetch one or more units from the array (random) */echo' ---------- array_rand () random ----------
'; $ ArrZ = array ("a", "B", "c", "d", "e"); var_dump (array_rand ($ arrZ )); // int (1) echo'
'; Var_dump (array_rand ($ arrZ, 3); // array (3) {[0] => int (0) [1] => int (1) [2] => int (3)} echo'
'; $ RandValue = array_rand ($ arrZ); $ result = is_string ($ randValue); // judge whether $ randValue is a string var_dump ($ result); // bool (false) echo'
';/*** Shuffle () disrupt the array */echo' ---------- shuffle () disrupt ----------
'; $ ArrZ = array ("a", "B", "c", "d", "e"); shuffle ($ arrZ); print_r ($ arrZ ); // Array ([0] => c [1] => B [2] => d [3] => e [4] => a) echo'
';/*** Array_sum () calculates the sum (sum) of all values in the array */echo' ---------- array_sum () summation ----------
'; $ ArrH = array (1, 3, 4, 5, 6, 7, 8, 9); echo array_sum ($ arrH); // 45 echo'
';/*** Range () creates an array containing the specified range units */echo' ---------- range () sum ----------
'; $ ArrR = range (0, 10); print_r ($ arrR ); // Array ([0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10) echo'
'; $ ArrR = range (0, 10, 3); // The third parameter represents the step (skip) print_r ($ arrR ); // Array ([0] => 0 [1] => 3 [2] => 6 [3] => 9) echo'
';/*** Array_fill () fill the array with the given value */echo' ---------- array_fill () sum ----------
'; $ ArrF = array_fill (0, 5, "iwanghang"); print_r ($ arrF ); // Array ([0] => iwanghang [1] => iwanghang [2] => iwanghang [3] => iwanghang [4] => iwanghang) echo'
';

The above is the PHP development (25) for Android programmers-array operation-related functions (3) grouping and arrangement-PhpStorm content. For more information, see PHP Chinese network (www.php1.cn )!

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.