PHP array Definition and destruction _php tutorial

Source: Internet
Author: User
Tags array definition
Defining the data in PHP is the most commonly used array () to achieve, of course, if you do not know the length of the array can be like array[] to define the unknown length of the data, if you want to clear the data we can directly unset (Array[key]) or directly complex value NULL.

Data definition

The code is as follows Copy Code

......
$array = Array (' One ', ' one ', ' one ', ' three ');
Var_dump ($array);
......

In the first line of this code fragment, we define a one-dimensional array $array, and in the second row we format the output of this array, and the result is this:

The code is as follows Copy Code

Array (3) {
[0]=>
String (3) "One"
[1]=>
String (3) "both"
[2]=>
String (5) "three"
}

Now the brother explains this output, first the first line of the output of this array,
Array (3) tells us that this is an array of three elements,
First, the No. 0 element is a string with a length of 3 (String (3)) ...
The length of the relationship is two left to say, roar ... what does this mean? This means that in PHP if we do not specify the subscript for the array, then he will be his own self-initiated from scratch to get us a subscript-that is, the key name in the PHP array; and then look at the following example:

The code is as follows Copy Code
......
$array = Array (' One ', "hello" = = ' ", ' three ');
Var_dump ($array);

...... This time when we define the array, we specify the subscript for the second element (since then, it is called the key name in this article, the subscript is a bit vague!). ), the second element specifies the key name (hello), let's look at the output:

The code is as follows Copy Code

Array (3) {
[0]=>
String (3) "One"
["Hello"]=>
String (3) "both"
[1]=>
String (5) "three"
}

I think you also like me to see a very intelligent phenomenon, the first element of the key name or 0--this we can understand, because we do not specify, and the PHP array must have a key name, PHP has to start from scratch to create a key name, the second element when we specify the key name, PHP uses this key name in respect of our opinions; complex things in the third element,

The third element, which looks very simple, we do not specify the key name, PHP automatically put the maximum integer key name plus 1, as the key name. But have you thought about it, if we change the key name of the first element to "-5", the second element has the same key name, what will the result be? We wait and see:

The code is as follows Copy Code
......
$array = Array ( -5=> ' one ', "hello" = = ' ", ' three ');
Var_dump ($array);
......

If you take it for granted that the third element's key name should be-4, then I'll tell you that the idea was right before PHP4.3.0, but then it was wrong, and now that's the version after PHP4.3.0, you'll see the following result:

The code is as follows Copy Code

Array (3) {
[-5]=>
String (3) "One"
["Hello"]=>
String (3) "both"
[0]=>
String (5) "three"
}

Yes, the third element is starting from 0, that is, no matter how small your negative number, the next element if you let PHP to define the key name, then he is starting from 0-remember this place is that in the existing key name if the largest is still a negative number, no matter how small negative numbers, PHP also from zero to start the next key name.

The destruction of the PHP array is simple, like destroying other variables.
Destroy entire array: unset ($array)
Destroys an element in an array: unset ($array [-5])

http://www.bkjia.com/PHPjc/628975.html www.bkjia.com true http://www.bkjia.com/PHPjc/628975.html techarticle defining data in PHP is the most common use of array () to achieve, of course, if you do not know the length of the array can be like array[] so to define the unknown length of data, if you want to clear the data we ...

  • 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.