'] = "30 ";$ Ages ['job'] = "34 ";Echo "Peter is". $ ages ['Peter ']. "years old .";?>Output of the above script:Peter is 32 years old.
Multi-dimensional arrayIn a multi-dimensional array, each element in the main array is also an array. Each element in the sub-array can also be an array, and so on.
Example 1In this example, we create a multidimensional array with an automatically assigned ID key:
The Code is as follows:
Copy code
$ Families = array("
; 17)
?>
Of course, no value in the array is allowed, that is, an empty array:
The code is as follows:
$ Result = array ();
?>
2. use the compact () function to create an array
The compact () function in PHP can convert one or more variables into arrays.
Definition format:
Array compact (var1, var2 ...)
Example 1: Any string without the corresponding variable name is skipped.
The code is as follows:
$ Firstname = "Peter ";
$ Lastname = "Griffin
[2]. "' S Neighbors "; /*how to ask Hovertree.com*/?>2. Associative arrays:Example 1$ages=Array("Peter" =>32, "Joe" =>30, "Lily" =>28); Example 2 This example is the same as Example 1, just another way to create an array. $ages["Peter"] = "32"; $ages["Joe"] = "30"; $ages["Lily"] = "28"; To use associative arrays in a script:PHP$ages["Peter"] = "32"; $ages["Joe"] = "30"; $ages["Lily"] = "28"; Echo"Peter is".$ages["Peter"]. " Years old. "; /*how to ask Hovertree.com*/?>above script output: Peter
; 17)?>
Of course, no value in the array is allowed, that is, an empty array:
The code is as follows:
$ Result = array ();?>
2. use the compact () function to create an array
The compact () function in PHP can convert one or more variables into arrays.
Definition format:
Array compact (var1, var2 ...)
Example 1: Any string without the corresponding variable name is skipped.
The code is as follows:
$ Firstname = "Peter ";$ Lastname = "Griffin
] = + D)
3. Of course, under certain specific requirements, some variables need to be stored in an array, the variable names of those variables are converted to key names, and the values of the variables are converted to key values (provided that the variable exists):
$firstname = "Peter"; $lastname = "Griffin"; $age = "38"; $result = Compact ("FirstName", "LastName", "age"); Print_r ($result); Print: Array ([FirstName] + peter [LastName] =
This article for you to share six more useful PHP array function, very good, with a certain reference value, need to refer to a friend
1. Array_column returns the value of a single column in the input array.2. Array_filter filters the elements in the array with a callback function.3. Array_map the user-defined function to each value on the given array, returning the new value.4. array_walk_recursive applies the user function recursively to each member in the array.5. Extract (import variables f
:
$result = Array ();
?>
2. Create an array using the compact () function
The compact () function in PHP can convert one or more variables into an array
Define the format:
Array Compact (VAR1,VAR2 ...)
Example 1: Any string that has no variable name corresponding to it is skipped.
Copy CodeThe code is as follows:
$firstname = "Peter";
$lastname = "Griffin";
$age = "38";
$result = Compact ("FirstName", "LastName", "age");
Print_r ($result);
?>
one-dimensional array.
The instance code is as follows:
functionmy_sort($a,$b){if($a==$b)return0;return($a>$b)?-1:1;}$people=array("Swanson"=>"Joe","Griffin"=>"Peter","Quagmire"=>"Glenn","swanson"=>"joe","griffin"=>"peter","quagmire"=>"glenn");uksort($people,"my_sort");print_r($people);?>输出:Array([swanson]=>joe[quagmire]=>glenn[griffin]=>peter[Swanson]=
the following example, we manually allocate a digital ID key.
$ Names [0] = "Peter ";$ Names [1] = "Joe ";$ Names [2] = "Lily ";
You can use these ID keys in the script:
2. join array:
Example 1
$ Ages = array ("Peter" => 32, "Joe" => 30, "Lily" => 28 );
Example 2
This example is the same as example 1. it is just another method for creating arrays.
$ages["Peter"] = "32"; $ages["Joe"] = "30"; $ages["Lily"] = "28";
Use the associated array in the script:
The above script output:
Peter is 32 ye
AJAX can be used to return database information in XML.
AJAX Database to XML instance (test Note: the instance feature is not implemented)
In the following AJAX example, we will show how the Web page reads information from the MySQL database, transforms the data into an XML document, and uses the document to display the information in different places.
This example is very similar to the example of "PHP AJAX Database" in the previous section, but there is a big difference: In this case, we
'] = "30 ";$ Ages ['job'] = "34 ";
You can use the ID key in the script:
The code is as follows:
$ Ages ['Peter '] = "32 ";$ Ages ['quagmire'] = "30 ";$ Ages ['job'] = "34 ";Echo "Peter is". $ ages ['Peter ']. "years old .";?>
Output of the above script:Peter is 32 years old.Multi-dimensional array In a multi-dimensional array, each element in the main array is also an array. Each element in the sub-array can also be an array, and so on.Example 1In this example, we create a multidimensional ar
"; $names [2] = "Lily"; Echo $names [0]. " and ". $names [1]. " Is ". $names [2]. "' S neighbors "; /* */?>
2. Associative arrays:
Example 1 $ages Array ("Peter" =>32, "Joe" =>30, "Lily" =>28); Example 2 This example is the same as Example 1, just another way to create an array. $ages ["Peter"] = "+"; $ages ["Joe"] = "a"; $ages ["Lily"] = ""; Use associative arrays in scripts:
php $ages["Peter"] = " +"; $ages ["Joe"] = "a"; $ages ["Lily"] = "";
in the script:Copy codeThe Code is as follows: $ Ages ['Peter '] = "32 ";$ Ages ['quagmire'] = "30 ";$ Ages ['job'] = "34 ";
Echo "Peter is". $ ages ['Peter ']. "years old .";?>
Output of the above script:
Peter is 32 years old.
Multi-dimensional array In a multi-dimensional array, each element in the main array is also an array. Each element in the sub-array can also be an array, and so on.
Example 1In this example, we create a multidimensional array with an automatically assigned ID
";Echo "Peter is". $ ages ['Peter ']. "years old .";?>Output of the above script:Peter is 32 years old. Multi-dimensional arrayIn a multi-dimensional array, each element in the main array is also an array. Each element in the sub-array can also be an array, and so on.Example 1In this example, we create a multidimensional array with an automatically assigned ID key: The code is as follows:Copy code $ Families = array("
And sometimes we ask for more complex sorting. such as the key name ordering, where the use of Ksort ($arr); function, which is sorted according to the key name of the array and maintains the original key-value relationship. The corresponding asort ($arr); function, which is to sort key values and maintain the original key-value relationship.
Same principle, rsort (); Arsort (); Krsort (); Functions in addition to sorting are sorted in descending order, others with sort (); Rsort (); Ksort ();
example, we manually allocate a digital ID key.
$ Names [0] = "Peter ";$ Names [1] = "Joe ";$ Names [2] = "Lily ";
You can use these ID keys in the script:
2. join array:
Example 1
$ Ages = array ("Peter" => 32, "Joe" => 30, "Lily" => 28 );
Example 2
This example is the same as example 1. it is just another method for creating arrays.
$ages["Peter"] = "32"; $ages["Joe"] = "30"; $ages["Lily"] = "28";
Use the associated array in the script:
The above script output:
Peter is 32 years old.
usage
The Uasort () function sorts the array by using the user-defined comparison function and keeps the index associated (not assigning a new key to the element).
Returns TRUE if successful, otherwise returns FALSE.
This function is primarily used to sort the associative arrays that are important to the order of the cells.
Grammar
Uasort (array,sorttype) parameter description
Array required. Specify the array to sort.
function Required. User-defined functions.
The function must be designed to
usage
The Uasort () function sorts the array by using the user-defined comparison function and keeps the index associated (not assigning a new key to the element).
Returns TRUE if successful, otherwise returns FALSE.
This function is primarily used to sort the associative arrays that are important to the order of the cells.
Grammar
Uasort (array,sorttype) parameter description
Array required. Specify the array to sort.
function Required. User-defined functions.
The function must be designed to
Code is as follows:
$ Language = array (1 => "PHP", 3 => "JAVA", 4 => "C ");$ Student = array ("name" => "James", "age" => 17)?>
Of course, no value in the array is allowed, that is, an empty array:
The Code is as follows:
$ Result = array ();?>
2. Use the compact () function to create an array
The compact () function in PHP can convert one or more variables into arrays.
Definition Format:
Array compact (var1, var2 ...)
Example 1: Any string without the corresponding variable name
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.