Definition and destruction of PHP arrays

Source: Internet
Author: User
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 you want to clear the data, you can directly unset (array [key]) or directly reset the value to null.

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 you want to clear the data, you can directly unset (array [key]) or directly reset the value to null.

The data definition instance code is as follows:

  1. ......
  2. $ Array = array ('one', 'two', 'Three ');
  3. Var_dump ($ array );
  4. ......

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 instance code is as follows:

  1. Array (3 ){
  2. [0] =>
  3. String (3) "one"
  4. [1] =>
  5. String (3) "two"
  6. [2] =>
  7. String (5) "three"
  8. }

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 instance code is as follows:

  1. ......
  2. $ Array = array ('one', "hello" => 'two', 'Three ');
  3. 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 instance code is as follows:

  1. Array (3 ){
  2. [0] =>
  3. String (3) "one"
  4. ["Hello"] =>
  5. String (3) "two"
  6. [1] =>
  7. String (5) "three"
  8. }

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 instance code is as follows:

  1. ......
  2. $ Array = array (-5 => 'one', "hello" => 'two', 'Three ');
  3. Var_dump ($ array );
  4. ......

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 instance code is as follows:

  1. Array (3 ){
  2. [-5] =>
  3. String (3) "one"
  4. ["Hello"] =>
  5. String (3) "two"
  6. [0] =>
  7. String (5) "three"
  8. }

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])

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.