This article will introduce some knowledge about custom functions and arrays in PHP Development. For more information, see. This article will introduce some knowledge about custom functions and arrays in PHP Development. For more information, see.
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: 2*3 = 6
The following is a variable parameter function.
"; // 2*1*2 = 4echo fun($p, 3)."
"; // 2*3*2 = 12echo fun($p, 3, 3)."
"; // 2*3*3 = 18?>
Output result:
4
12
18
Let's take a look at Custom Function Reference transfer.
"; // 2*1*2 = 4 echo fun($p, 3)."
"; // 2*3*2 = 12 echo fun($p, 3, 3)."
"; // 2*3*3 = 18 */ function fun(&$n){ $n=$n*$n; } $p=2; fun($p); echo $p; ?>
Output result: 4
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
// First
$ Arr1 = array (11, 22, 33, "44 ");
// Type 2
$ Arr2 = array ('a' => '11', 'B' => '22 ');
// The third type
$ 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(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.
2.
$ A = array ("a" => "Cat", "B" => "Dog", "c" => "Horse ");
3.
Print_r (array_change_key_case ($ a, CASE_UPPER ));
4.
?>
5.
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 that can be checked again when used.