Php array processing (1/3)

Source: Internet
Author: User
Tags arrays compact php tutorial

Php Tutorial array processing details
 
1.1 Create and initialize an array:
1. The arrary () function creates an array. By default, element 0 is the first element of the array,
The count () and sizeof () functions obtain the number of data elements.
2. Use variables to create an array
Compact () searches for the variable name in the current symbol table and adds it to the output array. The variable name becomes the key name and the variable content becomes the key value.
Copy the code as follows:
<? Php
$ Num = 10;
$ Str = "string ";
$ Array = array (1, 2, 3 );
$ Newarray = compact ("num", "str", "array ");
Print_r ($ newarray );
/* Result
Array ([num] = 10 [str] => string [array] => array ([0] => 1 [1] => 2 [2] => 3 ))
*/
?>

Extract () converts cells in the array into variables.
Copy the code as follows:
<? Php
$ Array = array ("key1" => 1, "key2" = 2, "key3" = 3 );
Extract ($ array );
Echo "$ key1 $ key2 $ key3"; // output 1 2 3
?>

3. Create an array using two arrays
Copy the code as follows:
Array_combine (array $ keys, array $ values)
<? Php
$ A = array ('green', 'red', 'yellow ');
$ B = array ('volcado', 'apple', 'bana ');
$ C = array_combine ($ a, $ B );
Print_r ($ c );
?>

4. Create a specified range array
Range ()
5. Automatically create an array
1.2 Key name and value operations
This section only describes common
. Check whether a key name and value exist in the array. Array_key_exists () and in_arrary functions. isset () checks the key names in the array. If the key name is NULL, isset () returns false, while array_key_exists () returns true.
. The array_search () function is used to check whether the key value of the array exists. If no key exists, NULL is returned.
. The key () function can obtain the key name of the current cell of the array.
. List () function, which assigns the value of the array to the specified variable. It is very useful in array traversal.
<? Php
$ Arr = array ("Red", "Blue", "White ");
List ($ red, $ blue, $ white) = $ arr;
Echo $ red; // red
Echo $ blue; // blue
Echo $ white; // white
. Array_fill () and array_fill_keys () can be filled with the value and key names of the array with the given duty schedule.
. Array_filp () can exchange the key names and values in the array. In addition, if the exchanged array has the same value, the same value is converted to the key name, and the value is retained to the last one.
. The array_keys () and array_values () functions can obtain the key names and values in the array and save them to a new array.
. Array_splice (arry $ input, int $ offset [, int $ length [, array $ replacement]) deletes one or more cells in the array and replaces them with other values.
. Array_unique () can be used to remove duplicate values from the array and return a new array without damaging the original array.
1.3 array traversal and output
1. Use the while loop to access the array
Apply the while, list (), and each () functions to traverse arrays.
2. for loop access array
3. Use foreach to access the array cyclically
Copy the code as follows:
<? Php
$ Color = array ("a" => "red", "blue", "white ");
Foreach ($ color as $ value)
{
Echo $ value. "<br>"; // outputs the value of the array.
}
Foreach ($ color as $ key => $ value)
{
Echo $ key. "=>". $ value. "<br>"; // key name and value of the output array
}
?>

Homepage 1 2 3 Last page
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.