PHP array creation, modification, and deletion operations

Source: Internet
Author: User
If the Boolean variable true is used as the key name, integer 1 is the key value. if faIse is used as the key name, integer 0 is the key value. If NULL is used as the key name, it is equivalent to NULL characters.

If the Boolean variable true is used as the key name, integer 1 is the key value. if faIse is used as the key name, integer 0 is the key value. If NULL is used as the key name, it is equivalent to using a NULL string, and using a NULL string as the key name will create or overwrite a new one with an empty string as the key value. Note: the array and object cannot be used as the key name. this will cause a warning of Illegal offset type.

In PHP programming, you can assign values to the array by specifying the key name in square brackets to change an existing array. In addition, you can add an empty square brackets ("[]") to the variable name to omit the key name. The basic syntax structure is as follows:
$ Arr [key] = value;
$ Arr [] = value;
// Key can be integer or String
// Value can be any value

If the array does not exist, a new array is created. To change a value, you only need to assign a new value to it. To delete a key name and value pair, use the unset function to release the variable. See Program 2-9.

Example of modifying the array key value in programs 2-9
$ Arr = array (5 => 1, 12 => 2 );
$ Arr [] = 3; // and $ arr [13] = 3; same effect
$ Arr ["x"] = 4; // add a new array element whose key value is x
Unset ($ arr [5]); // delete an array element with a key value of 5 from the array
Unset ($ arr); // delete the entire array
?>

Note:

As described in procedures 2-9, if only the variable name is followed by brackets and the key value name is not specified, the program will take the current maximum integer key value name 12, the new key value name will be the value plus 1. If the current array does not have an integer index, the key value is 0. If the specified key value already exists in the previous declaration, the original value corresponding to the key value name will be overwritten.

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.