Php core technology-array operators and array-related functions

Source: Internet
Author: User
Array_merge () when the element with the same subscript appears: If the index subscript is the same, the subscript of the array ($ arr2) element is incremented according to the subscript of $ arr1, merge. If the associated subscript is the same, the following elements will overwrite the value array operator of the elements on the front edge: +: merge operation of the array $ arr1 + $ arr2, but note: if $ arr1 and $ arr2 have the same element subscript, the value of $ arr1 is retained. Compare Array_merge (); the function used to merge arrays. Array_merge ($ arr1, $ arr2 );

Array_merge () when the element with the same subscript appears: If the index subscript is the same, the subscript of the array ($ arr2) element is incremented according to the subscript of $ arr1, merge. If the associated subscript is the same, the following element overwrites the value of the element on the front edge.


$arr1 = array('a', 'b', 'c', 'd');  $arr2 = array('i', 'ii', 'iii', 'iv');  $arr3 = array('a' => 1, 'b' => 2, 'c'=> 3, 'd'=>4);  $arr4 = array('a' => 'one', 'b' => 'two', 'c' => 'three');  var_dump($arr1 + $arr2);  echo '
  ';  var_dump($arr1 + $arr3);  echo '
  ';  var_dump(array_merge($arr1, $arr2));  echo '
  ';  var_dump(array_merge($arr1, $arr3));  echo '
  ';  var_dump(array_merge($arr3, $arr4)); 


= Equal

If the same key-value pair exists in the array, it is considered equal. The number of elements is the same, and each element has the same subscript and value.


$arr5 = array('a' => 1, 'b' => 2);  $arr6 = array(1, 2);//0=>1 1=>2  $arr7 = array('b' => 2, 'a' => 1);  var_dump($arr5 == $arr6);//false  var_dump($arr6 == $arr7);//false    var_dump($arr5 == $arr7);//true  var_dump($arr5 === $arr7);//false  


= Equal, the order of elements must be the same on the basis of equality.


var_dump($arr5 == $arr7);//true  ==  var_dump($arr5 === $arr7);//false  ===  


Array-related processing functions: Range (); create an array containing the specified Range element. For example, you need to create an array containing all lowercase letters.

Create an array that must contain all letters and numbers (not 0 ).


// Create a set of all possible characters $ chars = array_merge (range ('A', 'z'), range ('A', 'z'), range (1, 9 ));


Array_rand (); random acquisition of one or more units from the array. A random subscript is obtained, not a random element value. To obtain a random element value, you must use a random subscript to obtain the relative value.


Array_rand ($ arr, $ n); // randomly obtain four element subscript $ rand_keys = array_rand ($ chars, 4 );


Obtain the random four element values in the $ chars array:


// Shuffle ($ rand_keys) in a disordered order; // shuffle // traverses all the subscripts and obtains the corresponding character foreach ($ rand_keys as $ key) {// echo $ key, ''; echo $ chars [$ key],'';}


Shuffle () disrupts the order of array elements:


// Shuffle ($ rand_keys) in a disordered order; //


Character of the verification code:


// Create a set of all possible characters $ chars = array_merge (range ('A', 'z'), range ('A', 'z'), range (1, 9); // Obtain 4 element subscripts randomly $ rand_keys = array_rand ($ chars, 4); // shuffle ($ rand_keys) in a disordered order ); // disrupt // traverse all the subscript and obtain the corresponding character foreach ($ rand_keys as $ key) {// echo $ key ,''; echo $ chars [$ key], '';}


For details about array functions, refer:
Array key value operation function: Array_values (); get all values in the array Array_keys (); get all keys of the array

Array_combine ($ arr1, $ arr2) merges to generate an array, but uses the value of the first array element as the subscript of the new array, the value of the second array element is used as the value of the element of the new array.


$arr2 = array('a', 'b', 'c');  $arr3 = array('i', 'ii', 'iii');  var_dump(array_combine($arr2, $arr3));  //array(3) { ["a"]=> string(1) "i" ["b"]=> string(2) "ii" ["c"]=> string(3) "iii" }  echo '
 ';


Note the difference with array_merge.
Array_flip () swap the subscript and value of an array element:
$arr1 = array('a' => 1, 'b' => 2, 3, 4);  var_dump(array_flip($arr1));  //array(4) { [1]=> string(1) "a" [2]=> string(1) "b" [3]=> int(0) [4]=> int(1) }  echo '
  ';


Array_key_exists (); check whether the subscript exists

In_array (); check whether the value exists


$arr1 = array('a' => 1, 'b' => 2, 3, 4);var_dump(array_key_exists('a', $arr1));//true  echo '';  var_dump(in_array('a', $arr1));//false  echo '';  


Array merging and splitting: Array_chunk (array to be split, split size );
$arr1 =array('a', 'b', 'c', 'd', 'e');  var_dump(array_chunk($arr1, 2));  //array(3) { [0]=> array(2) { [0]=> string(1) "a" [1]=> string(1) "b" }  [1]=> array(2) { [0]=> string(1) "c" [1]=> string(1) "d" }  [2]=> array(1) { [0]=> string(1) "e" } }  
Array_merge ($ arr, $ arr2, $ arr3); merges multiple arrays at the same time. Note that the subscript is the same.

Extract (); registers the array element to the current variable list, uses the subscript of the element as the variable name, and uses the element value as the variable value. The return value is the number of variables registered in the variable list, because not all elements can be registered as variables, depending on whether the subscript of the element is a valid variable name rule.


$ Arr2 = array ('a' => 'I', 3 => 'II', 'C' => 'II'); var_dump ($ ); // output variable a value NULL var_dump (extract ($ arr2); var_dump ($ a, $ c); // string (1) "I" string (2) "ii" string (3) "iii"


Compared with extract, Compact () uses a bunch of variables to generate an array.

Explode (), which splits a string into an array.


$ Str1 = _ DIR __; var_dump ($ str1); echo ''; var_dump (explode ('\', $ str1); // array (5) {[0] => string (2) "E:" [1] => string (3) "amp" [2] => string (6) "apache" [3] => string (6) "htdocs" [4] => string (4) "test"} // one instructor teaches multiple courses $ teacher = array ('name' => 'xiaowang ', 'subobjects' => 'Mathematics-Chinese-physical '); var_dump (explode ('-', $ teacher ['subjects']); // array (3) {[0] => string (6) "mathematics" [1] => string (6) "Chinese" [2] => string (6) "physical "}


Implode (), linking an array element into a string.


$ Subjects = array ('mat', 'Physical ', 'chemically', 'Sport'); var_dump (implode ('-', $ subjects )); // string (27) "Mathematics-Physics-Chemistry-Sports



Intersection union set difference set

Array_diff difference set:


$arr1 = array('a', 'b', 'c', 'd');  $arr2 = array('b', 'd', 'e', 'f');  var_dump(array_diff($arr1, $arr2));  //array(2) { [0]=> string(1) "a" [2]=> string(1) "c" }  echo '';  


Array_intersect () intersection:


$arr1 = array('a', 'b', 'c', 'd');  $arr2 = array('b', 'd', 'e', 'f');  var_dump(array_intersect($arr1, $arr2));  //array(2) { [1]=> string(1) "b" [3]=> string(1) "d" }  


Simulate data structure stack and queue operations: Stack: first-in-first-out. Array_push array_pop

Array_push () The return value of the inbound stack is the number of elements in the array after the inbound stack.


$ Arr = array ('A', 'B', 'C'); // Input stack var_dump (array_push ($ arr, 'D ')); // int (4) var_dump ($ arr); // array (4) {[0] => string (1) "a" [1] => string (1) "B" [2] => string (1) "c" [3] => string (1) "d "}


Array_pop (); the returned value of the output stack is the value of the pop-up element.


// Output stack var_dump (array_pop ($ arr); // string (1) "c" var_dump ($ arr); // array (2) {[0] => string (1) "a" [1] => string (1) "B "}


Queue: first-in-first-out array_push array_shift


$arr = array();  array_push($arr, 'a', 'b', 'c');  var_dump($arr);  echo '';  var_dump(array_shift($arr));  //string(1) "a"  var_dump($arr);  //array(2) { [0]=> string(1) "b" [1]=> string(1) "c" }  


At the same time, an operation that can put data in the array header is provided: Array_unshift (); sorting related function:

Sort () sorts the array based on the value of the array element:


$arr = array(10, 20, 6, 8, 11);  sort($arr);  var_dump($arr); 


K indicates sorting by subscript, r indicates descending order, and a indicates retaining the natural order of subscript: note the differences between img2 and img11.

Natsort


$arr2 = array('img1.jpg', 'img2.jpg', 'img11.jpg');  sort($arr2);  var_dump($arr2);  //array(3) { [0]=> string(8) "img1.jpg" [1]=> string(9) "img11.jpg"  [2]=> string(8) "img2.jpg" }  $arr2 = array('img1.jpg', 'img2.jpg', 'img11.jpg');  natsort($arr2);  var_dump($arr2);  //array(3) { [0]=> string(8) "img1.jpg" [1]=> string(8) "img2.jpg"  [2]=> string(9) "img11.jpg" }  


Count (); Count the number of elements in the array. Count and for are used to traverse index arrays: number of recursive statistical elements is supported. Note: When count is used to Count non-array data, the returned result is 1 Array_search ($ ele, $ arr) searches for elements in the array and returns the subscript. otherwise, false is returned. how can we use array_search to determine whether an element exists in the array: Array_sum () to calculate the sum of the elements in the array:
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.