Php array summary _ PHP Tutorial

Source: Internet
Author: User
Php array summary. An array syntax defines an array. array () can be created using the array () structure. It accepts any number of key-value pairs separated by commas. Array (keyvalue an array syntax
Define array ()
You can use the array () language structure to create an array. It accepts any number of key => value pairs separated by commas.
Array (key => value
,...
)
// The key is an integer or string)
// Value can be of any type
$ Arr = array ("foo" => "bar", 12 => true );
Echo $ arr ["foo"]; // bar
Echo $ arr [12]; // 1
?>
The key can be an integer or string. If the key is a standard expression of an integer, it is interpreted as an integer (for example, "8" will be interpreted as 8, and "08" will be interpreted as "08 "). The floating point number in the key is rounded to integer. In PHP, the index array and the associated array are the same. they can both contain the subscript of the integer and string.
The value can be of any PHP type.
$ Arr = array ("somearray" => array (6 => 5, 13 => 9, "a" => 42 ));
Echo $ arr ["somearray"] [6]; // 5
Echo $ arr ["somearray"] [13]; // 9
Echo $ arr ["somearray"] ["a"]; // 42
?>
If no key name is specified for the given value, the current maximum integer index value is used, and the new key name is the value plus one. If the specified key name already has a value, the value will be overwritten.
// This array is the same as the following array...
Array (5 => 43, 32, 56, "B" => 12 );
//...
Array (5 => 43, 6 => 32, 7 => 56, "B" => 12 );
?>
Use TRUE as the key name to make integer 1 a key name. Use FALSE as the key name to make integer 0 the key name. Using NULL as the key name is equivalent to using a NULL string. Use an empty string as the key name to create (or overwrite) a new value with an empty string as the key name, which is different from the empty square brackets.
Arrays and objects cannot be used as keys ). This will cause a warning: Illegal offset type.
Create/modify with square brackets
You can explicitly set a value to change an existing array.
This is achieved by specifying a key name in square brackets to assign values to the array. You can also omit the key name. in this case, add an empty square brackets ("[]") to the variable name.
$ Arr [key] = value;
$ Arr [] = value;
// The key can be integer or string.
// Value can be of any type. if $ arr does not exist, a new value is created. This is also a replacement method for defining arrays. To change a value, just assign it a new value. If you want to delete a key/value pair, you need to use unset () for it ().
$ Arr = array (5 => 1, 12 => 2 );
$ Arr [] = 56; // This is the same as $ arr [13] = 56;
// At this point of the script
$ Arr ["x"] = 42; // This adds a new element
// The array with key "x"

Unset ($ arr [5]); // This removes the element from the array
Unset ($ arr); // This deletes the whole array
?>
Note:
As mentioned above, if square brackets are provided but no key name is specified, the current maximum integer index value is taken. the new key name is the value + 1. If no integer index exists, the key name is 0. If the specified key name already has a value, the value will be overwritten.
Note that the maximum integer key used here is not necessarily in the array currently. It only needs to exist after the index is re-generated in the last array. The following is an example:
// Create a simple array
$ Array = array (1, 2, 3, 4, 5 );
Print_r ($ array );
// Delete all elements, but keep the array unchanged:
Foreach ($ array as $ I =>$ value ){
Unset ($ array [$ I]);
}
Print_r ($ array );
// Add a unit (note that the new key name is 5, rather than the value 0 you may think)
$ Array [] = 6;
Print_r ($ array );
// Re-index:
$ Array = array_values ($ array );
$ Array [] = 7;
Print_r ($ array );
?>
The above routine will output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Array
(
)
Array
(
[5] => 6
)
Array
(
[0] => 6
[1] => 7
2. array functions
1. array merging:
Array array_merge (array1.array2 );
2. delete the last element in the array, and bring up the last element of the array.
Mixed array_pop (array $ array );
$ Array must be an array variable
Returned value: mixed: the deleted element.
3. press data into the end of the array
Int array_push (array $ array, mixed var [, mixed...])
First parameter: array must be an array variable (passed by reference)
The second parameter: var [, mixed...] can insert multiple variables.
Return value: int is the starting position of the corresponding number.
4. randomly select one or more elements from the array
Mixed array_rand (array input [, int num_req]);
First parameter: input array
The second parameter: num_req specifies the quantity to be selected.
Returned value: mixed is the subscript of the array. if num_req> 1, the returned value is the int array of the following table.
5. delete the first element of the array.
Mixed array_shift (array $ array)
The first parameter: array must be an array variable (passed by reference );
Returned value: mixed deleted element value
6. return the sum of all values in the array.
If an array element is a number, the sum of the elements in the array is returned. if a string exists, it is converted to an integer.
Number array_sum (array );
The first parameter: array must be an array variable;
Return value: number
7. check whether a value exists in the array.
Bool in_array (mixed needle, array haystack [, bool strict])
Needle variable to be searched
Haystack array
Strict

Author: "ITeamsky-Yang Bo's technical space"

The arrays array syntax defines the array (). you can use the array () language structure to create an array. It accepts any number of key = value pairs separated by commas. Array (key = value...

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.