PHP array definition and destruction _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags array definition
Define and destroy PHP arrays. We usually use array () to define data in php. if you do not know the length of the array, you can define the length of the unknown data as array, if we want to clear the data, we define the data in php. The most common method is array, of course, if you do not know the length of the array, you can define the length of unknown data like array []. to clear the data, you can directly unset (array [key]) or directly return null.

Data Definition

The code is as follows:

......
$ Array = array ('one', 'two', 'Three ');
Var_dump ($ array );
......

In the first line of the code snippet, a one-dimensional array $ array is defined. in the second line, the array is formatted and output. The result is as follows:

The code is as follows:

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

Now, let's explain the output result. First, the first line of the output result of this array,
Array (3) tells us that this is an array with three elements,
Element 0 is a 3-character string (3 ))...
Let's not talk about the remaining two links ......, What does this mean? This means that if we do not specify a subscript for the array in PHP, he will give us a subscript from scratch, that is, the key name in the PHP array; let's take a look at the example below:

The code is as follows:
......
$ Array = array ('one', "hello" => 'two', 'Three ');
Var_dump ($ array );

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

The code is as follows:

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

I think you see a very intelligent phenomenon like me. the key name of the first element is still 0. we can understand this because we didn't specify it, the PHP array must have a key name, and PHP creates a key name from scratch. when the second element is used, we specify the key name, PHP uses this key name to respect our opinions. the complicated thing lies in the third element,

The third element looks very simple. we didn't specify a key name. PHP automatically adds 1 to the maximum integer key name as the key name. But you have never thought about it. if we change the key name of the first element to "-5" and the key name of the second element remains unchanged, what will the result be? We will wait and see:

The code is as follows:
......
$ Array = array (-5 => 'one', "hello" => 'two', 'Three ');
Var_dump ($ array );
......

If you take it for granted that the key name of the third element should be-4, I will tell you that this idea was correct before PHP4.3.0, but then it will be wrong, now, PHP4.3.0 and later versions, you will see the following results:

The code is as follows:

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

Yes, the third element starts from 0, that is, no matter how small your negative number is, if the next element asks PHP to define the key name, then he starts from 0. remember that in the current key name, if the maximum value is still a negative number, no matter how small the negative number is, PHP is also the next key name from scratch.

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

If you do not know the length of the array, you can define the length of the unknown data like array []. if you want to clear the data, we will...

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.