Common functions for PHP operation arrays

Source: Internet
Author: User

Common functions for/* *  arrays  * 1. The key/value operation function of the array  * array_values ()-  returns all values in the array  * array_ Keys () returns some or all of the key names in the array  * in_array ()   checks if a value exists in the array   returns &NBSP;TRUE if  needle  is found, otherwise returns  false.  * array_search -  searches the array for the given value and returns the corresponding key name if successful  * array_key_exists -  Checks whether the given key name or index exists in the array  *  array_flip -  keys and values in the interchange array  *  array_reverse -   Returns an array of cells in the reverse order of  *2. Array elements in a statistic array  *count -  counts the number of cells in an array or the number of attributes in an object   *array_count_ values -  counts the number of occurrences of a value in an array    returns an associative array, using the value in the  input  array as the key name, and the number of occurrences in the array as the value.  *array_unique -  Move the duplicate value  returns the filtered array.  *3 in the divisor group. Functions for handling arrays using callback functions  *array_filter -  filtering cells in arrays with callback functions   *array_walk -  Use a user-defined function to perform callback processing for each element in the array   *array_map -  to function the callback function on the cell of the given    * */ // array_values -  returns all the values in the array $arr = [' Wang Xiaoming ', ' Jin Sha Harbour ', ' small Hong Kong '], $a =array_values ($arr); Var_dump ($a); $lamp =array (     ' OS ' = ' Linux ',      ' WebServer ' + ' Apache ',     ' DB ' = ' MySQL ',      ' Language ' + ' php ',     ' lang ' = ' php ',     ' num1 ' =>10,      ' num2 ' + ',//     ' Girl ' =>null) var_dump (Array_values ($lamp ) List ($OS, $WebServer, $DB, $Language)  = array_values ($lamp);echo  $OS; $b =array_keys ($lamp);// Returns all key names $c=array_keys ($lamp, ' Apache ');//returns Apache key name $d=array_keys ($lamp, 10,true);//return 10  plus true  Represents the same value and type as 10. ===  must be integral type   var_dump ($b), Var_dump ($d),//in_arrayif (In_array (' MySQL ', $lamp)) {     echo  ' in the array ';} else{    echo  ' not in array ';} array_search -  searches the array for the given value, and if successful returns the corresponding key name  var_dump (Array_search (' MySQL ', $lamp));//array_key_ exists -  checks whether the given key name or index exists in the array if (array_key_exists (' DB', $lamp)) {    echo  ' in the array ';} else{    echo  ' not in array ';} Array_key_exists the value of the array is null to return Trueif (array_key_exists (' Girl ', $lamp)) {    echo  ' in the array ' ;} else{    echo  ' not in array ';} Isset ()   determines that the value of the array is null to return Falseif (Isset ($lamp [' Girl '], $lamp)) {    echo  ' in the array ';} else{    echo  ' not in array ';} array_flip -  the keys and values in the interchange array var_dump (Array_flip ($lamp));//array_reverse -  returns an array of cells in reverse order   Var_dump (Array_reverse ($lamp));//If set to  TRUE  retains the key of the number.   Non-numeric keys are not affected by this setting and are always retained.   $arr 1 = [    1=>1,    3=>3,     5=>4    ];var_dump (Array_reverse ($arr 1,true)); =================================== ========================================= //count -  count the number of cells in an array or the number of properties in an object   $STR = ' i am  yangyihai '; Var_dump (count ($str));//int 1$str1=[&nbsP; 1=>[1,2,3],  2=>[2,4,6]];echo count ($str 1);//2   default statistics outer array echo count ($STR);//8   If the optional  mode  parameter is set to  count_recursive (or  1), COUNT ()   will recursively count the array     Outer plus inner layer $str2 = [1,1,2,2,2,3,5,7];//array_count_values -  the number of occurrences of all values in the statistics array    Returns an associative array, using the value in the  input  array as the key name, and the number of occurrences in the array as the value. Var_dump (Array_count_values ($str 2));//array_unique -  the duplicate value in the divisor group  returns the filtered  array. var_dump (Array_unique ($str 2));//array_filter -  filters the cells   $arr in the array with a callback function  =  [1,2,4,null, "", -5-4-2,-4,3,false,true,3,4, ' a '];var_dump ($arr); Var_dump (Array_filter ($arr)); function  calltest ($a) {    if ($a >=0) {        return  true;    }else{        return false;     }}function odd ($var) {   return ($var &NBSP;&AMP;1);//odd   with 1 fetch and operation} $arr 1 = [1,2,3,4,5,6,7,8,9];//var_dump (Array_filter ($arr, ' calltest ')); var_ Dump (Array_filter ($arr 1, ' odd ')), Var_dump (Array_values (Array_filter ($arr 1,function  ($var) {     return (! ( $var  & 1)) (//Even}));//array_walk -  Use the user-defined function to do callback processing for each element in the array Function walkcall (& $var) {    echo  $var = $var * $var. '   ';} In a typical case  funcname  accepts two parameters. The value of the array  parameter as the first, the key name as the second.  function myfun ($value, $key) {    echo  "{$key}===>{$value}";} Array_walk ($arr 1,  ' Walkcall ') array_walk ($arr 1,  ' myfun ');//Use anonymous function Array_walk ($arr 1,function (&$ Value, $key) {     $value = $value +1;}); Array_walk ($arr 1,function (& $value, $key, $str) {     $value = $value +1;     echo  $key. $str. $value;}, ' ———— ');//var_dump ($arr 1); Function cube ($var) {     return  $var * $var * $VAR;} $arr 2 = [1,2,3,4,5,6,7,8,9];var_dump (Array_map (' cube ', $arr 2)), $arr 3 = [1,2,3,4]; $arr 4 = [' one ', ' one ', ' Three ', ' Four '];function mapcall ($a, $b) {    echo  "$a-----$b <br/>";} $rarr =array_map (' Mapcall ',  $arr 3, $arr 4), Var_dump ($rarr); $rarr 1=array_map (null,  $arr 3, $arr 4); var_dump ($rarr 1);


Common functions for PHP operation arrays

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.