A detailed description of custom functions and array examples in PHP

Source: Internet
Author: User
Tags array definition
One, custom function

Custom functions are our own defined functions, and the Custom function format in PHP is as follows:

function Funname (arg1, arg2, Arg3 ...) {//todoreturn values;}
<?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:
Here's another variable parameter function.

<?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 =?>

Let's see. 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 =     echo Fun ($p, 3, 3). <br> ";    2*3*3 =      /function Fun (& $n) {          $n = $n * $n;      }      $p =2;      Fun ($p);      echo $p;  ? >

Second, array definition assignment

1, array basic writing format

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

Array ("AA", N, 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  $arr 1=array (one, three, three, "seven");  The second  $arr 2=array (' a ' = ' one ', ' b ' = ' + ');  The third kind of  $arr 3[0]= ';  $arr 3[1]= ' 30 ';

Three, array operation

1. Modification

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

2. Delete

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

3. Use

$arr =array (one, one, one, one), 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 key and value echo $id: $value. " <br> ";}

Four or two-d arrays

$arr =array (Array ("1", "one", "111"), Array ("2", "A", "222"), Echo $arr [to];

Five, array functions

(1) array_change_key_case (array, case)

Array: required, arrays.

Case: Optional, Case_lower (default, lowercase returns the key of the array), Case_upper (the key of the array is returned in uppercase letters)

Function: Converts all keys of an array to uppercase or lowercase.

<?php $a =array ("a" = "Cat", "b" = "Dog", "C" and "Horse"); Print_r (Array_change_key_case ($a, case_upper));?>  Result: Array ([a] = Cat [B] = Dog [C] = + Horse)

(2) Array_chunk (Array,size,preserve_key)

Array: Required.

Size: Required to specify how many elements each new array includes.

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

Function: Divides an array into new array blocks.

<?php      //array_chunk (array,size,preserve_key)      $a 1=array ("a" and "Cat", "B" and "Dog", "c" = "Horse", "D" = "Cow");      Print_r (Array_chunk ($a));      $a 2=array ("a" = "Cat", "b" = "Dog", "c" = "Horse", "D" and "Cow");      Print_r (Array_chunk ($a 2,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, which can be checked again when used, and the list is as follows (PHP represents the first version)

function Description PHP
Array () Creates an array. 3
Array_change_key_case () Returns an array whose keys are uppercase or lowercase. 4
Array_chunk () Splits 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 the array. 4
Array_diff () Returns an array of difference sets for two arrays. 4
ARRAY_DIFF_ASSOC () Compares key names and key values, and returns an array of two arrays of differences. 4
Array_diff_key () Compares a key name and returns an array of two array of differences. 5
ARRAY_DIFF_UASSOC () The error set of an array is computed by indexing the user-supplied callback function. 5
Array_diff_ukey () Use a callback function to compare the difference set of an array to a key name. 5
Array_fill () Fills the array with the given value. 4
Array_filter () Filters the elements in the array with a callback function. 4
Array_flip () Swaps 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 the key value, and returns an array of two arrays of intersections. 4
Array_intersect_key () Computes the intersection of an array using the key name comparison. 5
ARRAY_INTERSECT_UASSOC () An index check computes the intersection of an array and compares the index with a callback function. 5
Array_intersect_ukey () Use the callback function to compare the key names to calculate the intersection of the arrays. 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 scoped to the cell of the given array. 4
Array_merge () Merges one or more arrays into an array. 4
Array_merge_recursive () Merges one or more arrays recursively. 4
Array_multisort () Sorts multiple arrays or multidimensional arrays. 4
Array_pad () Fills the array with a value to the specified length. 4
Array_pop () POPs the last cell of the array (out of the stack). 4
Array_product () Computes the product of all the values in the array. 5
Array_push () Presses one or more cells (elements) into the end of the array (into the stack). 4
Array_rand () Randomly selects one or more elements from the array and returns them. 4
Array_reduce () Iteratively simplifies the array to a single value using a 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 the 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 the array and returns it as a condition. 4
Array_splice () Remove parts of the array and replace them with other values. 4
Array_sum () Computes the and of all values in the array. 4
Array_udiff () Use the callback function to compare the data to calculate the difference set of the array. 5
ARRAY_UDIFF_ASSOC () The index check calculates the difference set of the array and compares the data with the callback function. 5
ARRAY_UDIFF_UASSOC () The index check calculates the difference set of the array, using the callback function to compare the data and the index. 5
Array_uintersect () Computes the intersection of an array and compares the data with a callback function. 5
ARRAY_UINTERSECT_ASSOC () With the index check to compute the intersection of the array, compare the data with the callback function. 5
ARRAY_UINTERSECT_UASSOC () With an index check computes the intersection of an array, using a callback function to compare data and indexes. 5
Array_unique () Removes duplicate values from 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 () Applies a user function to each member in an array. 3
Array_walk_recursive () Each member of the array is recursively applied to the user function. 5
Arsort () Reverse-Sort an array and maintain an index relationship. 3
Asort () Sorts the array and maintains the index relationship. 3
Compact () Create an array, including 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 the array. 3
each () Returns the current key/value pair in the array and moves the array pointer one step forward. 3
End () Points the inner pointer of the array 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 array is reversed by key name. 3
Ksort () Arrays are sorted by key name. 3
List () Assigns the values in the array to some variables. 3
Natcasesort () Use the "natural sort" algorithm to sort the case-insensitive letters of an array. 4
Natsort () Use the "natural sort" algorithm to sort the array. 4
Next () Moves the inner pointer in the array forward one bit. 3
POS () The alias of current (). 3
Prev () Returns the internal pointer of the array back to one bit. 3
Range () Creates an array of elements that contain the specified range. 3
Reset () Points the inner pointer of the array to the first element. 3
Rsort () Reverse-order an array. 3
Shuffle () Rearrange the elements in the array in random order. 3
sizeof () The alias of Count (). 3
Sort () Sorts the array. 3
Uasort () Use a user-defined comparison function to sort the values in the array and keep the index associated. 3
Uksort () Use a user-defined comparison function to sort the key names in the array. 3
Usort () Use a user-defined comparison function to sort the values in the array. 3


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.