Home >Backend Development >PHP Tutorial >Custom functions and arrays for PHP mobile Internet development

Custom functions and arrays for PHP mobile Internet development

巴扎黑
巴扎黑Original
2017-03-19 09:31:561138browse

[Introduction] 1. Custom functions Custom functions are functions defined by ourselves. The format of custom functions in PHP is as follows: function funname(arg1, arg2, arg3 ){ TODOreturn values;}

# custom function is the function of our own definition. The custom function format in PHP 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 result:

Custom functions and arrays for PHP mobile Internet development

Here is another function with variable parameters

	<?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 = 12  
    echo fun($p, 3, 3)."<br>";    // 2*3*3 = 18  
?>

Custom functions and arrays for PHP mobile Internet development

Let’s take a look at custom function reference passing

	<?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 = 12 
    echo fun($p, 3, 3)."<br>";    // 2*3*3 = 18 
    */  
      
    function fun(&$n){  
        $n=$n*$n;  
    }  
    $p=2;  
    fun($p);  
    echo $p;  
?>

Custom functions and arrays for PHP mobile Internet development

1. Basic writing format of array

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

array("aa", 12, true, 2.2, "test", 50); //Get data through array subscript

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

array("title"=>"aa", "age"=>20); //Only by key name Obtain data

2. How to create an array

	//第一种  
$arr1=array(11, 22, 33, "44");  
//第二种  
$arr2=array(&#39;a&#39;=>&#39;11&#39;, &#39;b&#39;=>&#39;22&#39;);  
//第三种  
$arr3[0]=&#39;20&#39;;  
$arr3[1]=&#39;30&#39;;

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. Traversal

$arr=array('a'=>11, 'b'=>22, 'c'=>33, 'd'=>44);

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

echo $value."
";

}

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

echo $id..$value."
";

}

$arr=array(array("1","11","111"), array("2","22","222"));

echo $arr[1 ][2];

(1)array_change_key_case(array, case)

array: required, array.

case: Optional, CASE_LOWER (default value, lowercase letters return the keys of the array), CASE_UPPER (uppercase letters return the keys of the array)

Function: Convert all KEYs of the array to Uppercase or lowercase.

  1. 	<?php  
        $a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");  
        print_r(array_change_key_case($a,CASE_UPPER));  
    ?>   
    结果:Array ( [A] => Cat [B] => Dog [C] => Horse )

##(2)array_chunk(array,size,preserve_key)

array: required.

size: Required, specifies how many elements each new array contains.

preserve_key: optional, true (preserve key name), false (new index)

Function: Divide an array into new array blocks.

	<?php  
    //array_chunk(array,size,preserve_key)  
      
    $a1=array("a"=>"Cat","b"=>"Dog","c"=>"Horse","d"=>"Cow");  
    print_r(array_chunk($a1,2));  
      
    $a2=array("a"=>"Cat","b"=>"Dog","c"=>"Horse","d"=>"Cow");  
    print_r(array_chunk($a2,2,true));  
      
?>   
结果:

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 many functions like this, you can check them when you use them. The list is as follows (php represents the first version)

Function describe PHP
array() Create an array. 3
array_change_key_case() Returns an array whose keys are all uppercase or lowercase. 4
array_chunk() Split an array into new array blocks. 4
array_combine() Create a new array by merging two arrays. 5
array_count_values() Used to count the number of occurrences of all values ​​in the array. 4
array_diff() Return the difference array of two arrays. 4
array_diff_assoc() Compare the key name and key value, and return the difference array of the two arrays. 4
array_diff_key() Compare key names and return the difference array of the two arrays. 5
array_diff_uassoc() Calculate the difference set of the array by doing index checking through the callback function provided by the user. 5
array_diff_ukey() Use the callback function to compare the key names and calculate the difference set of the array. 5
array_fill() Fill the array with the given values. 4
array_filter() Use the callback function to filter the elements in the array. 4
array_flip() Exchange keys and values ​​in the array. 4
array_intersect() Calculate the intersection of arrays. 4
array_intersect_assoc() Compare the key name and key value, and return the intersection array of the two arrays. 4
array_intersect_key() Use key name comparison to calculate the intersection of arrays. 5
array_intersect_uassoc() Calculate the intersection of arrays with index checking, and use the callback function to compare the indexes. 5
array_intersect_ukey() Use the callback function to compare key names to calculate the intersection of arrays. 5
array_key_exists() Check whether the given key name or index exists in the array. 4
array_keys() Return all key names in the array. 4
array_map() Apply the callback function to the cells of the given array. 4
array_merge() Combine one or more arrays into one array. 4
array_merge_recursive() Recursively merge one or more arrays. 4
array_multisort() Sort multiple arrays or multidimensional arrays. 4
array_pad() Pall the array to the specified length with values. 4
array_pop() Pop the last unit of the array (pop off the stack). 4
array_product() Calculate the product of all values ​​in the array. 5
array_push() Push one or more units (elements) to the end of the array (push). 4
array_rand() Randomly select one or more elements from the array and return. 4
array_reduce() Use the callback function to iteratively reduce the array to a single value. 4
array_reverse() Reverse the order of elements in the original array, create a new array and return it. 4
array_search() Search for the given value in the array and return the corresponding key name if successful. 4
array_shift() Delete the first element in the array and return the value of the deleted element. 4
array_slice() Retrieve a value from the array based on conditions and return it. 4
array_splice() Remove part of the array and replace it with other values. 4
array_sum() Calculate the sum of all values ​​in the array. 4
array_udiff() Use callback function to compare data to calculate the difference of arrays. 5
array_udiff_assoc() With index check, calculate the difference set of the array, and use the callback function to compare the data. 5
array_udiff_uassoc() With index check, calculate the difference set of the array, and use the callback function to compare the data and index. 5
array_uintersect() Calculate the intersection of arrays and use callback functions to compare data. 5
array_uintersect_assoc() Calculate the intersection of arrays with index checking, and use callback functions to compare data. 5
array_uintersect_uassoc() With index check, calculate the intersection of arrays, and use callback function to compare data and index. 5
array_unique() Delete duplicate values ​​in the array. 4
array_unshift() Insert one or more elements at the beginning of the array. 4
array_values() Return all values ​​in the array. 4
array_walk() Apply the user function to each member of the array. 3
array_walk_recursive() Apply the user function recursively to each member of the array. 5
arsort() Reverse sort the array and maintain the index relationship. 3
asort() Sort the array and maintain the index relationship. 3
compact() Create an array including variable names and their values. 4
count() Calculate the number of elements in the array or the number of attributes in the object. 3
current() Return the current element in the array. 3
each() Returns the current key/value pair in the array and moves the array pointer forward one step. 3
end() Point the internal pointer of the array to the last element. 3
extract() Import variables from the array into the current symbol table. 3
in_array() Check whether the specified value exists in the array. 4
key() Get the key name from the associative array. 3
krsort() Sort the array in reverse order by key name. 3
ksort() Sort the array by key name. 3
list() Assign the values ​​in the array to some variables. 3
natcasesort() Use the "natural sorting" algorithm to sort the array in a case-insensitive manner. 4
natsort() Sort the array using the "natural sorting" algorithm. 4
next() Move the internal pointer in the array forward one bit. 3
pos() Alias ​​for current(). 3
prev() Rewind the internal pointer of the array by one bit. 3
range() Create an array containing elements in the specified range. 3
reset() Point the internal pointer of the array to the first element. 3
rsort() Sort the array in reverse order. 3
shuffle() Rearrange the elements in the array in random order. 3
sizeof() An alias for count(). 3
sort() Sort the array. 3
uasort() Use a user-defined comparison function to sort the values ​​in the array and maintain index association. 3
uksort() Use user-defined comparison function to sort the key names in the array. 3
usort() Use user-defined comparison function to sort the values ​​in the array. 3

1. Custom function

Custom functions are functions we define ourselves. The format of custom functions in PHP 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 result:

Custom functions and arrays for PHP mobile Internet development

Here is another function with variable parameters

	<?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 = 12  
    echo fun($p, 3, 3)."<br>";    // 2*3*3 = 18  
?>
Custom functions and arrays for PHP mobile Internet development

Let’s take a look at custom function reference passing

	<?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 = 12 
    echo fun($p, 3, 3)."<br>";    // 2*3*3 = 18 
    */  
      
    function fun(&$n){  
        $n=$n*$n;  
    }  
    $p=2;  
    fun($p);  
    echo $p;  
?>
Custom functions and arrays for PHP mobile Internet development

2. Array definition and assignment

1. Basic writing format of array

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

array("aa", 12, true, 2.2, "test", 50); //Get data through array subscript

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

array("title"=>"aa", "age"=>20); //Data can only be obtained through key names

2. How to create an array

	//第一种  
$arr1=array(11, 22, 33, "44");  
//第二种  
$arr2=array(&#39;a&#39;=>&#39;11&#39;, &#39;b&#39;=>&#39;22&#39;);  
//第三种  
$arr3[0]=&#39;20&#39;;  
$arr3[1]=&#39;30&#39;;

3. Array operations

1. Modification

$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. Traversal

$arr=array('a'=>11, 'b'=>22, 'c'=>33, 'd'=>44);

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

echo $value."
";

}

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

echo $id..$value."
";

}

4. Two-dimensional array

$arr=array(array("1","11","111"), array("2","22","222"));

echo $arr[1][2];

5. Array function

(1)array_change_key_case(array, case)

array: required, array.

case: optional, CASE_LOWER (default value, lowercase letters return the key of the array), CASE_UPPER (uppercase letters return the key of the array)

Function: Convert all KEYs in the array to uppercase or lowercase.

  1. 	<?php  
        $a=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");  
        print_r(array_change_key_case($a,CASE_UPPER));  
    ?>   
    结果:Array ( [A] => Cat [B] => Dog [C] => Horse )

(2)array_chunk(array,size,preserve_key)

array: required.

size: Required, specifies how many elements each new array contains.

preserve_key: optional, true (preserve key name), false (new index)

Function: Divide an array into new array blocks.

	<?php  
    //array_chunk(array,size,preserve_key)  
      
    $a1=array("a"=>"Cat","b"=>"Dog","c"=>"Horse","d"=>"Cow");  
    print_r(array_chunk($a1,2));  
      
    $a2=array("a"=>"Cat","b"=>"Dog","c"=>"Horse","d"=>"Cow");  
    print_r(array_chunk($a2,2,true));  
      
?>   
结果:

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 many functions like this, you can check them when using them, the list is as follows (php represents the first version)

Function describe PHP
array() Create an array. 3
array_change_key_case() Returns an array whose keys are all uppercase or lowercase. 4
array_chunk() Split an array into new array blocks. 4
array_combine() Create a new array by merging two arrays. 5
array_count_values() Used to count the number of occurrences of all values ​​in the array. 4
array_diff() Return the difference array of two arrays. 4
array_diff_assoc() Compare the key name and key value, and return the difference array of the two arrays. 4
array_diff_key() Compare key names and return the difference array of the two arrays. 5
array_diff_uassoc() Calculate the difference set of the array by doing index checking through the callback function provided by the user. 5
array_diff_ukey() Use the callback function to compare the key names and calculate the difference set of the array. 5
array_fill() Fill the array with the given values. 4
array_filter() Use the callback function to filter the elements in the array. 4
array_flip() Exchange keys and values ​​in the array. 4
array_intersect() Calculate the intersection of arrays. 4
array_intersect_assoc() Compare the key name and key value, and return the intersection array of the two arrays. 4
array_intersect_key() Use key name comparison to calculate the intersection of arrays. 5
array_intersect_uassoc() Calculate the intersection of arrays with index checking, and use the callback function to compare the indexes. 5
array_intersect_ukey() Use the callback function to compare key names to calculate the intersection of arrays. 5
array_key_exists() Check whether the given key name or index exists in the array. 4
array_keys() Return all key names in the array. 4
array_map() Apply the callback function to the cells of the given array. 4
array_merge() Combine one or more arrays into one array. 4
array_merge_recursive() Recursively merge one or more arrays. 4
array_multisort() Sort multiple arrays or multidimensional arrays. 4
array_pad() Pall the array to the specified length with values. 4
array_pop() Pop the last unit of the array (pop off the stack). 4
array_product() Calculate the product of all values ​​in the array. 5
array_push() Push one or more units (elements) to the end of the array (push). 4
array_rand() Randomly select one or more elements from the array and return. 4
array_reduce() Use the callback function to iteratively reduce the array to a single value. 4
array_reverse() Reverse the order of elements in the original array, create a new array and return it. 4
array_search() Search for the given value in the array and return the corresponding key name if successful. 4
array_shift() Delete the first element in the array and return the value of the deleted element. 4
array_slice() Retrieve a value from the array based on conditions and return it. 4
array_splice() Remove part of the array and replace it with other values. 4
array_sum() Calculate the sum of all values ​​in the array. 4
array_udiff() Use callback function to compare data to calculate the difference of arrays. 5
array_udiff_assoc() With index check, calculate the difference set of the array, and use the callback function to compare the data. 5
array_udiff_uassoc() With index check, calculate the difference set of the array, and use the callback function to compare the data and index. 5
array_uintersect() Calculate the intersection of arrays and use callback functions to compare data. 5
array_uintersect_assoc() Calculate the intersection of arrays with index checking, and use callback functions to compare data. 5
array_uintersect_uassoc() With index check, calculate the intersection of arrays, and use callback function to compare data and index. 5
array_unique() Delete duplicate values ​​in the array. 4
array_unshift() Insert one or more elements at the beginning of the array. 4
array_values() Return all values ​​in the array. 4
array_walk() Apply the user function to each member of the array. 3
array_walk_recursive() Apply the user function recursively to each member of the array. 5
arsort() Reverse sort the array and maintain the index relationship. 3
asort() Sort the array and maintain the index relationship. 3
compact() Create an array including variable names and their values. 4
count() Calculate the number of elements in the array or the number of attributes in the object. 3
current() Return the current element in the array. 3
each() Returns the current key/value pair in the array and moves the array pointer forward one step. 3
end() Point the internal pointer of the array to the last element. 3
extract() Import variables from the array into the current symbol table. 3
in_array() Check whether the specified value exists in the array. 4
key() Get the key name from the associative array. 3
krsort() Sort the array in reverse order by key name. 3
ksort() Sort the array by key name. 3
list() Assign the values ​​in the array to some variables. 3
natcasesort() Use the "natural sorting" algorithm to sort the array in a case-insensitive manner. 4
natsort() Sort the array using the "natural sorting" algorithm. 4
next() Move the internal pointer in the array forward one bit. 3
pos() Alias ​​for current(). 3
prev() Rewind the internal pointer of the array by one bit. 3
range() Create an array containing elements in the specified range. 3
reset() Point the internal pointer of the array to the first element. 3
rsort() Sort the array in reverse order. 3
shuffle() Rearrange the elements in the array in random order. 3
sizeof() An alias for count(). 3
sort() Sort the array. 3
uasort() Use a user-defined comparison function to sort the values ​​in the array and maintain index association. 3
uksort() Use user-defined comparison function to sort the key names in the array. 3
usort() Use user-defined comparison function to sort the values ​​in the array. 3

 

The above is the detailed content of Custom functions and arrays for PHP mobile Internet development. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn