Php Array Method Summary--example is the truth

Source: Internet
Author: User

//1 Replace the key value size

$arr = Array (' a ' = = 1, ' b ' = = 2, ' C ' =>3, ' D ' + 4);         $lower = Array_change_key_case ($arr, case_lower);       $upper = Array_change_key_case ($arr, Case_upper);      Var_dump ($lower); Var_dump ($upper);


2 Divide the array into $size arrays, Haskay to preserve the original key value

$arr 2 = Array (' name ' = ' Lixueyao ', ' age ' = ', ' sex ' = ' f ');      $size = 2;     $hasKey = true;     $chunk = Array_chunk ($arr 2, $size, $hasKey); Var_dump ($chunk);

3 You can generate a new array with the key specified in a multidimensional array, and the 3rd parameter can be the key value of the new array

$arr 3s = array (' name ' = ' Lixueyao ', ' age ' = ' + ', ' sex ' = ' f '), Array (' name ' = = ' Zhang       San ', ' age ', ' sex ' = ' m '), Array (' name ' = ' = ' lisi ', ' age ' = ' + ', ' sex ' = ' f ');     $column = Array_column ($arr 3s, ' age ', ' name '); Print_r ($column);


4 combine two arrays, forming a new array of one as value, one as key

$arrCombineOne = Array (' One ', ' one ', ' one ', ' three ');     $arrCombineTwo = Array (' 1 ', ' 2 ', ' 3 ');     $combine = Array_combine ($arrCombineTwo, $arrCombineOne); Print_r ($combine);


5 returns the number of occurrences of value in the array

$arrValue = Array (' C ', ' d ', ' a ', ' C ', ' V ');     $arrcnt = Array_count_values ($arrValue); Print_r ($ARRCNT);


//6 compares the difference set of the array value, the one and the parameter comparisons, and the difference between the non-existent in the same array (in the same and the other, the other is the array of comparisons)
If multiple arrays are compared, it is the difference set of 3 arrays. (You can compare multiple arrays)

$arrDiffOne = Array (' a ' = = ' red ', ' b ' = = ' blue ', ' c ' = ' black ', ' d ' = ' = ' ' purple ');     $arrDiffTwo = Array (' a ' = = ' red ', ' b ' = ' blue ', ' n ' = ' black ',);     $arrDiffThree = Array (' a ' + = ' red ', ' f ' = = ' Hello ', ' c ' = ' black ');     $arrDiff = Array_diff ($arrDiffOne, $arrDiffTwo, $arrDiffThree); Print_r ($arrDiff)

;

7 Compare the array's key and value (all the same), one is the array to be compared
If it is 3 arrays to compare, you can compare the two groups, and then use other arrays to compare the difference between the first two groups

$ARRASSOC = Array_diff_assoc ($arrDiffOne, $arrDiffTwo, $arrDiffThree); Print_r ($ARRASSOC);

8 Comparing the key value of an array, same principle

$a 4 = array (' d ' = = ' name ');     $arrKey = Array_diff_key ($arrDiffOne, $arrDiffTwo, $arrDiffThree, $a 4); Print_r ($arrKey);

 
 

  $uassocOne  = array (' name '  =>  ' Zsy ',  ' age ' =>24,  ' say '  =>   ' Hello ');      $uassocTwo  = array (' name '  =>  ' Lixueyao ',   ' age '  => 24);      function myfunction ($a  ,  $b)      {          $res  = null;          if ($a  ===  $b)           {              $res  = 0;         }           $res  =  ($a < $b)  ? 1:-1;           return  $res;     }      $arrUassoc  = array_diff_uassoc ($uassocOne, &NBSP; $uassocTwo,  ' myfunction ');      print_r ($ARRUASSOC); 


10 comparison Key

function Mydiffkey ($a, $b) {if ($a = = = $b) {return 0;          } return ($a > $b)? 1:-1;         } $result =array_diff_ukey ($uassocOne, $uassocTwo, "Mydiffkey"); Print_r ($result);




Php Array Method Summary--example is the truth

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.