Mobile Web Development tutorial Four, PHP custom functions and arrays

Source: Internet
Author: User
Tags array definition arrays comparison lowercase sorts

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;

}

The code is as follows Copy Code
<?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);
?>


Here's another function for a variable parameter

  code is as follows copy code
<?php  
/*
Function Fun ($m, $n) {
    if ($m ==0 | | $n ==0) {
     & nbsp;  return 0;
   }else{
        $a = $m * $n;
        return $a;
   }
}
 
$p = 2;
$h = 3; The
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 
?>


And take a look at the Custom Function reference pass

The code is as follows Copy Code
<?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;
?>


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 code is as follows Copy Code
First Kind
$arr 1=array (11, 22, 33, "44");
Second Kind
$arr 2=array (' A ' => ', ' B ' => ' 22 ');
Third Kind
$arr 3[0]= ' 20 ';
$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.

The code is as follows Copy Code
<?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.

The code is as follows Copy Code
<?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 () to create arrays. 3
Array_change_key_case () returns an array whose keys are either 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 () is 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 difference sets for two arrays. 4
Array_diff_key () compares the key names 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 the array with the callback function. 4
Array_flip () Swaps the keys and values in the array. 4
Array_intersect () computes the intersection of the array. 4
ARRAY_INTERSECT_ASSOC () compares the key names and key values and returns an array of intersections of two arrays. 4
Array_intersect_key () uses the key name to compare the intersection of the computed array. 5
ARRAY_INTERSECT_UASSOC () computes the intersection of the array with the index check, and compares the index with the 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 () functions The callback function to the cell of the given array. 4
Array_merge () merges one or more arrays into an array. 4
Array_merge_recursive () recursively merges one or more arrays. 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 units (elements) into the end of the array (into the stack). 4
Array_rand () randomly selects one or more elements from the array and returns. 4
Array_reduce () simplifies the array to a single value by iterating through 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 () Extracts a value in the array according to the condition and returns. 4
Array_splice () Removes a portion of the array and replaces it with other values. 4
Array_sum () computes the and of all the values in the array. 4
Array_udiff () uses the callback function to compare the data to compute the difference set of the array. 5
ARRAY_UDIFF_ASSOC () evaluates the difference set of the array with an index check and compares the data with the callback function. 5
ARRAY_UDIFF_UASSOC () evaluates the difference set of the array with the index check and compares the data and index with the callback function. 5
Array_uintersect () computes the intersection of the array, using the callback function to compare the data. 5
ARRAY_UINTERSECT_ASSOC () computes the intersection of the array with the index check, using the callback function to compare the data. 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 duplicate values 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 () applies a user function to each member of the array. 3
Array_walk_recursive () recursively applies user functions to each member of the array. 5
Arsort () To reverse-sort the array and keep the index relationship. 3
Asort () The array is sorted and the index relationship is maintained. 3
Compact () creates an array, including 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 present 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 array's internal pointer to the last element. 3
Extract () Imports the variable from the 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 sorted in reverse order by key name. 3
The Ksort () array is sorted by key name. 3
List () assigns the values in the array to some variables. 3
Natcasesort () uses the "natural sort" algorithm to sort the case-insensitive letters of the array. 4
Natsort () sorts the array using the natural sort algorithm. 4
Next () Moves the internal pointer in the array forward one bit. 3
Alias for POS () current (). 3
Prev () returns the internal pointer of the 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 () The reverse ordering of the array. 3
Shuffle () rearranges the elements in the array in random order. 3
Alias for sizeof () count (). 3
Sort () the array is sorted. 3
Uasort () uses the user-defined comparison function to sort the values in the array and keep the index associated. 3
Uksort () uses the user-defined comparison function to sort the key names in the array. 3
Usort () uses the 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.