PHP Mobile Internet Development Notes (4)--custom functions and arrays

Source: Internet
Author: User
Tags foreach format array array definition end functions sort variable

One, Custom function

The custom function is our own defined function, in PHP the custom function format is as follows:

function Funname (arg1, arg2, Arg3 ...) {

Todo

return values;

}

    1. 	<?php  
          function Fun ($m, $n) {  
              if ($m ==0  $n ==0) {return  
                  0;  
              } else{  
                  $a = $m * $n;  
                  return $a;  
              }  
            
          $p =2;  
          $h =3;  
          echo $p. " * ". $h." = ". Fun ($p, $h);  
      ? >  
Output results:

Here's another function for a variable parameter

	<?php 
    /* Function Fun ($m, $n) { 
        if ($m ==0  $n ==0) {return 
            0; 
        } else{ 
            $a = $m * $n; 
            return $a; 
        } 
     
    $p =2; 
    $h =3; 
    echo $p. " * ". $h." = ". Fun ($p, $h);  
      
      
    /function Fun ($m, $n =1, $x =2) {  
        $a = $m * $n * $x *;  
        return $a;  
    }  
      
    $p =2;  
    echo Fun ($p). " <br> ";          2*1*2 = 4  
    echo Fun ($p, 3). " <br> ";       2*3*2 =  
    echo Fun ($p, 3, 3). " <br> ";    2*3*3 =  
?>  

And take a look at the Custom Function reference pass

	<?php   
    /* Function Fun ($m, $n) { 
        if ($m ==0  $n ==0) {return 
            0; 
        } else{ 
            $a = $m * $n; 
            return $a; 
        } 
     
    $p =2; 
    $h =3; 
    echo $p. " * ". $h." = ". Fun ($p, $h);  
    * 
    /* Function Fun ($m, $n =1, $x =2) { 
        $a = $m * 
        $n * $x *; return $a; 
    } 
     
    $p =2; 
    echo Fun ($p). " <br> ";          2*1*2 = 4 
    echo Fun ($p, 3). " <br> ";       2*3*2 = 
    echo Fun ($p, 3, 3). " <br> ";    2*3*3 =  
      
    */function Fun (& $n) {  
        $n = $n * $n;  
    }  
    $p =2;  
    Fun ($p);  
    echo $p;  
? >  

Two, the array definition assigns the value

1. Array basic writing format

Simple form: Array (value 1, value 2, value 3, ...)

Array ("AA", A, true, 2.2, "test", 50); Get data by array subscript

Full form: Array (key 1=> value 1, key 2=> value 2, ...)

Array ("title" => "AA", "Age" =>20); Data can only be obtained by key name

2. How to create an array

	The first kind of $arr 1=array (one, three,  
"");  
The second type of  
$arr 2=array (' A ' => ', ' B ' => ');  
The third kind of  
$arr 3[0]= ';  
$arr 3[1]= ' 30 ';  

Three, array operation

1, modify

$arr =array (11, 22, 33, 44);

$arr [0]=55; The array becomes $arr=array (55, 22, 33, 44);

2, delete

$arr =array (11, 22, 33, 44);

unset ($arr [0]); The array becomes $arr=array (22, 33, 44);

3. Use

$arr =array (11, 22, 33, 44);

echo $arr [0];

$arr =array (' A ' =>11, ' B ' =>22, ' C ' =>33, ' d ' =>44);

echo $arr [' B ']];

4, Traverse

$arr =array (' A ' =>11, ' B ' =>22, ' C ' =>33, ' d ' =>44);

foreach ($arr as $value) {//No key Name

echo $value. " <br> ";

}

foreach ($arr as $id => $value) {//Output keys and values

echo $id. " __ ". $value." <br> ";

}

Four or two-D array

$arr =array (Array ("1", "One", "a"), Array ("2", "22", "222"));

echo $arr [1][2];

Five, array functions

(1) array_change_key_case (array, case)

Array: required, arrays.

Case: Optional, Case_lower (default value, lowercase letter returns array key), Case_upper (uppercase return array key)

Action: Converts all keys in an array to uppercase or lowercase.

    1. 	<?php  
          $a =array ("a" => "Cat", "B" => "Dog", "C" => "Horse");  
          Print_r (Array_change_key_case ($a, case_upper));  
      ? >   
      Results: Array ([A] => Cat [B] => Dog [C] => horse)
      

(2) Array_chunk (Array,size,preserve_key)

Array: Required.

Size: Required, specify how many elements each new array contains.

Preserve_key: Optional, True (keep key name), False (new index)

Function: Divide an array into new array blocks.

	<?php  
    //array_chunk (array,size,preserve_key)  
      
    $a 1=array ("a" => "Cat", "B" => "Dog", "C" => "horse", "D" => "Cow");  
    Print_r (Array_chunk ($a 1,2));  
      
    $a 2=array ("a" => "Cat", "B" => "Dog", "C" => "horse", "D" => "Cow");  
    Print_r (Array_chunk ($a 2,2,true));  
      
>   
Results:

Array ([0] => Array ([0] => Cat [1] => Dog) [1] => Array ([0] => horse [1] => Cow))
Array ([0] => Array ([a] => Cat [b] => Dog) [1] => Array ([C] => horse [d] => Cow))

.......

There are a lot of functions like this, which can be looked up when used, as shown in the following list (PHP represents the first occurrence of the version)

function Description PHP
Array () Creates an array. 3
Array_change_key_case () Returns an array whose keys are either uppercase or lowercase. 4
Array_chunk () Divides an array into a new array block. 4
Array_combine () Creates a new array by merging two arrays. 5
Array_count_values () Used to count the occurrences of all values in an array. 4
Array_diff () Returns an array of difference sets for two arrays. 4
ARRAY_DIFF_ASSOC () Compares the key name and key values and returns an array of difference sets for two arrays. 4
Array_diff_key () Compares the key name and returns an array of difference sets for two arrays. 5
ARRAY_DIFF_UASSOC () Computes the difference set of an array by indexing the user-supplied callback function. 5
Array_diff_ukey () Compares the difference set of an array with a callback function for the key name. 5
Array_fill () Fills the array with the given value. 4
Array_filter () Filters the elements in an array with a callback function. 4
Array_flip () Swap the keys and values in the array. 4
Array_intersect () Computes the intersection of an array. 4
ARRAY_INTERSECT_ASSOC () Compares the key name and key values and returns an array of intersections of two arrays. 4
Array_intersect_key () Computes the intersection of an array by using the key name comparison. 5
ARRAY_INTERSECT_UASSOC () Computes the intersection of an array with an index check and compares the index with a callback function. 5
Array_intersect_ukey () Computes the intersection of the array using the callback function to compare the key name. 5
Array_key_exists () Checks whether the given key name or index exists in the array. 4
Array_keys () Returns all the key names in the array. 4
Array_map () The callback function is acting on the cell of the given array. 4
Array_merge () Merges one or more arrays into an array. 4
Array_merge_recursive () Recursively merge one or more arrays. 4
Array_multisort () Sorts multiple arrays or multidimensional arrays. 4
Array_pad () Fills an array with a value to a specified length. 4
Array_pop () Eject the last cell of the array (out of the stack). 4
Array_product () Calculates the product of all the values in an array. 5
Array_push () Presses one or more units (elements) into the end of the array (into the stack). 4
Array_rand () Randomly selects one or more elements from an array and returns. 4
Array_reduce () Iteratively simplifies the array to a single value using the callback function. 4
Array_reverse () Flips the order of elements in the original array, creates a new array, and returns. 4
Array_search () Searches the array for a given value, and returns the corresponding key name if successful. 4
Array_shift () Deletes the first element in the array and returns the value of the deleted element. 4
Array_slice () Takes a value out of a condition in an array and returns it. 4
Array_splice () Remove a portion of the array and replace it with other values. 4
Array_sum () Calculates the and of all the values in the array. 4
Array_udiff () Use the callback function to compare data to compute the difference set of an array. 5
ARRAY_UDIFF_ASSOC () Calculates the difference set of an array with an index check and compares the data with a callback function. 5
ARRAY_UDIFF_UASSOC () Calculates the difference set of an array with an index check and compares the data and index with the callback function. 5
Array_uintersect () Computes the intersection of the array and compares the data with the callback function. 5
ARRAY_UINTERSECT_ASSOC () Computes the intersection of the array with the index check and compares the data with the callback function. 5
ARRAY_UINTERSECT_UASSOC () Computes the intersection of the array with the index check and compares the data and index with the callback function. 5
Array_unique () Deletes the duplicate value in the array. 4
Array_unshift () Inserts one or more elements at the beginning of the array. 4
Array_values () Returns all the values in the array. 4
Array_walk () Apply a user function to each member in the array. 3
Array_walk_recursive () Each member of an array applies a user function recursively. 5
Arsort () Reverse-Sort the array and keep the index relationship. 3
Asort () The array is sorted and the index relationship is maintained. 3
Compact () Create an array that includes the variable names and their values. 4
Count () Calculates the number of elements in an array or the number of attributes in an object. 3
Current () Returns the current element in an array. 3
each () Returns the current key/value pair in the array and moves the array pointer one step forward. 3
End () Points the array's internal pointer to the last element. 3
Extract () Imports a variable from an array into the current symbol table. 3
In_array () Checks whether the specified value exists in the array. 4
Key () Gets the key name from the associative array. 3
Krsort () The arrays are sorted in reverse order by key names. 3
Ksort () The array is sorted by key name. 3
List () Assign the values in the array to some variables. 3
Natcasesort () Use the "natural sort" algorithm to sort the case-insensitive letters of the array. 4
Natsort () Sort the array using the natural sort algorithm. 4
Next () Moves the internal pointer in the array forward one bit. 3
POS () The alias for current (). 3
Prev () Returns the internal pointer of an array to a bit. 3
Range () Creates an array that contains the elements of the specified range. 3
Reset () Points the array's internal pointer to the first element. 3
Rsort () Reverse-Sort the array. 3
Shuffle () Rearrange the elements in the array in random order. 3
sizeof () The alias for Count (). 3
Sort () Sort the array. 3
Uasort () Use the user-defined comparison function to sort the values in the array and keep the index associated. 3
Uksort () Use the user-defined comparison function to sort the key names in the array. 3
Usort () Use the user-defined comparison function to sort the values in the array. 3



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.