Array in PHP

Source: Internet
Author: User
The array in PHP is an ordered ing (1-to-1 relation key-value ). Array is a combination of arrays, dictionaries, and collections. The key can be int or string. Value can be of any type. The key is forcibly converted as follows: 1. A string integer that contains valid integer values. 88 actual storage 82. Floating Point integer. 8.78 decimal places

The array in PHP is an ordered ing (1-to-1 relation key-value ). Array is a combination of arrays, dictionaries, and collections. The key can be int or string. Value can be of any type. The key is forcibly converted as follows: 1. string containing valid integer values = integer. "8" = 8 actually stored 8 2. Floating Point = integer. 8.7 = 8 decimal places

The array in PHP is an ordered ing (1-to-1 Relationship key-> value ).
Array is a combination of arrays, dictionaries, and collections.

The key can be int or string. Value can be of any type.

The key is forcibly converted as follows:
1. a string containing valid integer values => integer. "8" => 8 actual storage 8
2. Floating Point => integer. 8.7 => 8 decimal places will be removed
3. boolean type => type. True => 1, false => 0 actually stores 0 or 1
4. Null => "" actual storage ""
5. arrays and objects cannot be used as key names.

Key names cannot be repeated. If they are repeated, only the last one is valid.

  "a",    "1"  => "b",    1.5  => "c",    true => "d",);var_dump($array);?>

The above routine will output:

array(1) {  [1]=>  string(1) "d"}

In the preceding example, all key names are forcibly converted1, Then each new unit will overwrite the previous value, and only one"D".

PHP Arrays can contain both int and string key names, because PHP does not actually distinguish between index arrays and associated arrays.

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.

Example #5 specify the key name only for some units

  "c",         "d",);var_dump($array);?>

The above routine will output:

Array (4) {[0] => // string (1) "a" [1] => string (1) by default) "B" [6] => string (1) "c" [7] => // if no key is specified, the previous index value + 1 string (1) "d "}

Array units can be accessed using the array [key] syntax.
Example #6 access an array Unit

  "bar",    42    => 24,    "multi" => array(         "dimensional" => array(             "array" => "foo"         )    ));var_dump($array["foo"]);var_dump($array[42]);var_dump($array["multi"]["dimensional"]["array"]);?>

The above routine will output:

string(3) "bar"int(24)string(3) "foo"

Note:

Square brackets and curly brackets can be used to access array units (for example, $ array [42] and $ array {42} have the same effect in the preceding example ).

To modify a value, assign a new value to the Unit by its key name. To delete a key-value pair, call the unset () function.

  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 to                // the array with key "x"                unset($arr[5]); // This removes the element from the arrayunset($arr);    // This deletes the whole array?>

Note: As described above, if square brackets are provided but no key name is specified, the current maximum integer index value is used. The new key name is the value plus 1 (but the minimum value is 0 ). If no integer index exists, the key name is 0. 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:

 $ Value) {unset ($ array [$ I]);} print_r ($ array); // Add a unit (note that the new key name is 5, instead of what you might think is 0) $ 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)

Convert to array

For any integer, float, string, boolean, and resource types, If you convert a value to an array, an array with only one element is obtained, and its subscript is 0, this element is the value of this scalar. In other words, (array) $ scalarValue is exactly the same as array ($ scalarValue.

If an object type is converted to array, the result is an array, and its unit is the attribute of the object. The key name is the member variable name, but there are several exceptions: the integer attribute is not accessible; the class name prefix is added before the private variable; the variable prefix is added before the protection variable. Each of these prefixes has a NULL character. This will lead to some unpredictable behaviors:

 

In the preceding example, two keys are named 'A', but one of them is '\ 0A \ 0a '.

Convert NULL to array to get an empty array.

Multi-purpose Array type. The following is an example:

Example #8 Use array ()

  4,              'OS'         => 'Linux',              'lang'       => 'english',              'short_tags' => true            );            // strictly numerical keys$array = array( 7,                8,                0,                156,                -10              );// this is the same as array(0 => 7, 1 => 8, ...)$switching = array(         10, // key = 0                    5    =>  6,                    3    =>  7,                     'a'  =>  4,                            11, // key = 6 (maximum of integer-indices was 5)                    '8'  =>  2, // key = 8 (integer!)                    '02' => 77, // key = '02'                    0    => 12  // the value 10 will be overwritten by 12                  );                  // empty array$empty = array();         ?>

Example #9 set

 

The above routine will output:

Do you like red?Do you like blue?Do you like green?Do you like yellow?

Example #10 change the unit in a loop

  $color) {    $colors[$key] = strtoupper($color);}print_r($colors);?>

The above routine will output:

Array(    [0] => RED    [1] => BLUE    [2] => GREEN    [3] => YELLOW)

Example #14 recursive and multi-dimensional arrays

 Array ("a" => "orange", "B" => "banana", "c" => "apple"), "numbers" => array (1, 2, 3, 4, 5, 6), "holes" => array ("first", 5 => "second", "third ")); // Some examples to address values in the array above echo $ fruits ["holes"] [5]; // prints "second" echo $ fruits ["fruits"] ["a"]; // prints "orange" unset ($ fruits ["holes"] [0]); // remove "first" // Create a new multi-dimen1_array $ juices ["apple"] ["Green"] = "good";?> The value assignment of an Array always involves copying values. Use the reference operator to copy an array through reference.
 
Related Article

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.