PHP mobile internet development notes (4) -- custom functions and arrays

Source: Internet
Author: User
1. user-defined function: user-defined function. in PHP, the user-defined function format is as follows: functionfunname (arg1, arg2, arg3) {TODOreturnvalues;} output result: let's take a variable parameter function and look at the UDF reference.

I. user-defined functions

Custom functions are defined by ourselves. the format of custom functions in PHP is as follows:

Function funname (arg1, arg2, arg3 ......){

// TODO

Return values;

}

  
Output result:

The following is a variable parameter function. Vc3ryb25np1_vcd48cd48l3a + PHByZSBjbGFzcz0 = "brush: java;"> "; // 2*1*2 = 4 echo fun ($ p, 3 )."
"; // 2*3*2 = 12 echo fun ($ p, 3, 3 )."
"; // 2*3*3 = 18?>

Let's take a look at Custom Function Reference transfer.

  ";  // 2*1*2 = 4echo fun($p, 3)."
";// 2*3*2 = 12echo fun($p, 3, 3)."
";// 2*3*3 = 18*/function fun(&$n){$n=$n*$n;}$p=2;fun($p);echo $p;?>

2. array definition assignment

1. basic array writing format

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

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

Complete format: array (key 1 => value 1, key 2 => value 2 ,......)

Array ("title" => "aa", "age" => 20); // You can only obtain data by key name.

2. create an array

// The first $ arr1 = array (11, 22, 33, "44"); // The second $ arr2 = array ('a' => '11 ', 'B' => '22'); // The Third $ arr3 [0] = '20'; $ arr3 [1] = '30 ';

3. Array Operations

1. modify

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

$ Arr [0] = 55; // The array is changed to $ arr = array (55, 22, 33, 44 );

2. delete

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

Unset ($ arr [0]); // The array is changed to $ 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 ."
";

}

Foreach ($ arr as $ id => $ value) {// output key and value

Echo $ id. "_". $ value ."
";

}

IV. two-dimensional array

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

Echo $ arr [1] [2];

5. array functions

(1) array_change_key_case (array, case)

Array: required, array.

Case: (optional) CASE_LOWER (default value: return the array key with lowercase letters) and CASE_UPPER (return the array key with uppercase letters)

Purpose: convert all keys of the array to uppercase or lowercase letters.

  "Cat","b"=>"Dog","c"=>"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, specifying the number of elements included in each new array.

Preserve_key: optional, true (retain key name), false (new index)

Function: convert a number component into a new array block.

   "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));?> 
Result:

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. the list is as follows (php indicates the first version)

Function Description PHP
Array () Create an array. 3
Array_change_key_case () Returns an array with uppercase or lowercase keys. 4
Array_chunk () Splits an array into new array blocks. 4
Array_combine () Create a new array by combining two arrays. 5
Array_count_values () Used to count the number of times all values appear in the array. 4
Array_diff () Returns the number of difference sets of two arrays. 4
Array_diff_assoc () Compare the key name and key value, and return the number groups of the two arrays. 4
Array_diff_key () Compare the key name, and return the number groups of the two arrays. 5
Array_diff_uassoc () You can use the callback function provided by the user to perform an index check to calculate the array's difference set. 5
Array_diff_ukey () Use the callback function to compare the difference set of the calculated array with the key name. 5
Array_fill () Fill the array with the given value. 4
Array_filter () Use the callback function to filter elements in the array. 4
Array_flip () Exchange keys and values in the array. 4
Array_intersect () Calculates 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 the key name to compare and calculate the intersection of arrays. 5
Array_intersect_uassoc () Check and calculate the intersection of arrays with indexes, and use the callback function to compare indexes. 5
Array_intersect_ukey () Use the callback function to compare the 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 () Returns all the key names in the array. 4
Array_map () The callback function acts on the units of the given array. 4
Array_merge () Combine one or more numbers into an array. 4
Array_merge_recursive () Recursively merges one or more arrays. 4
Array_multisort () Sorts multiple arrays or multi-dimensional arrays. 4
Array_pad () Fill the array with values to the specified length. 4
Array_pop () Bring up the last unit of the array (out stack ). 4
Array_product () Calculates the product of all values in the array. 5
Array_push () Push one or more units (elements) to the end of the array (into the stack ). 4
Array_rand () Randomly selects one or more elements from the array and returns the result. 4
Array_reduce () Use the callback function to iteratively simplify the array to a single value. 4
Array_reverse () Flip the elements in the original array sequentially, create a new array, and return it. 4
Array_search () Search for the given value in the array. if the value is successful, the corresponding key name is returned. 4
Array_shift () Delete the first element in the array and return the value of the deleted element. 4
Array_slice () Extract a value from the array based on the conditions and return it. 4
Array_splice () Remove part of the array and replace it with other values. 4
Array_sum () Calculates the sum of all values in the array. 4
Array_udiff () Use the callback function to compare data to calculate the difference set of the array. 5
Array_udiff_assoc () Calculate the difference set of the array with index check, and use the callback function to compare the data. 5
Array_udiff_uassoc () Use the callback function to compare data and indexes. 5
Array_uintersect () Calculate the intersection of arrays and use the callback function to compare the data. 5
Array_uintersect_assoc () Check and calculate the intersection of arrays with indexes, and use the callback function to compare data. 5
Array_uintersect_uassoc () Use the callback function to compare data and indexes. 5
Array_unique () Delete repeated values in the array. 4
Array_unshift () Insert one or more elements at the beginning of the array. 4
Array_values () Returns all values in the array. 4
Array_walk () Apply user functions to each member in the array. 3
Array_pai_recursive () Recursively apply user functions to each member in the array. 5
Arsort () Sort the array in reverse order and maintain the 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 () Point the internal pointer of the array to the last element. 3
Extract () Import the variable from the array to the current symbol table. 3
In_array () Check whether the specified value exists in the array. 4
Key () Obtain the key name from the joined array. 3
Krsort () Sort the array in reverse order by key name. 3
Ksort () Sort the array by key name. 3
List () Assign values in the array to some variables. 3
Natcasesort () Use the "natural sorting" algorithm to sort arrays by case-insensitive letters. 4
Natsort () Sort arrays using the "natural sorting" algorithm. 4
Next () Move the internal pointer in the array one by one. 3
Pos () The alias of current. 3
Prev () Returns the internal pointer of the array to one position. 3
Range () Creates an array containing elements in a specified range. 3
Reset () Points the internal pointer of the array to the first element. 3
Rsort () Sorts arrays in reverse order. 3
Shuffle () Sorts the elements in the array in random order. 3
Sizeof () The alias of count. 3
Sort () Sorts arrays. 3
Uasort () Use the custom comparison function to sort the values in the array and maintain the index Association. 3
Uksort () Sort the key names in the array using the custom comparison function. 3
Usort () Sort the values in the array using the user-defined comparison function. 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.