Detail not noticed in php5.2.x: array-PHP source code

Source: Internet
Author: User
Ec (2); I just read the php tutorial. Due to the php version problem, I found that there are some places in the array to study and make several experiments on & nbsp; php5.2.5: & nbsp; 1. $ arr & nbsp; array (& quot; a & quot; & nbsp; & gt; & nbsp; 1, & quot; B & quot; & nbsp; & gt; & nbsp; 2, & quot; c & quot; & nbsp; & gt; & script ec (2); script

I just read the php tutorial. Due to the php version, I found that there is a place to study the array.
We have made several experiments on php5.2.5:

1,



$ Arr = array ("a" => 1, "B" => 2, "c" => 3 );

However, if an array is defined like this, a compilation error is returned:



$ Arr = array ("a" = 1, "B" = 2, "c" = 3 );

Therefore, when defining arrays, you can only use =>

2,



$ Arr = array ("a" => 1, "B" => 2, "c" => 3 );
Echo $ arr [0];
Echo $ arr [1];

A blank space is displayed:



Echo $ arr ["a"];


3. When adding or modifying an element, you can only use =. You cannot use =>



$ Arr = array ("a" => 1, "B" => 2, "c" => 3 );
$ Arr ["c"] => 6;


It may be usable in earlier versions, but a compilation error occurs in 5.2.5.
To add or modify an element, write it as follows:



$ Arr = array ("a" => 1, "B" => 2, "c" => 3 );
$ Arr ["d"] = 4;
$ Arr ["c"] = 6;
To delete an element, use unset


Unset ($ arr ["c"]);

4. Create an experiment and guess what it is:



$ Arr = array ("a" => 1, 2, "B" => 3, 4 );
$ Arr [] = 5;
Foreach ($ arr as $ key => $ value)
{
Echo "key: $ key value: $ value
";
}

Result:

Key: a value: 1
Key: 0 value: 2
Key: B value: 3
Key: 1 value: 4
Key: 2 value: 5

This makes it clear that php automatically uses a number starting from 0 as the key only when the user does not have a key defined.

5. arrays in php have pointers. You can perform forward and backward operations on arrays.



$ Arr = array ("a" => 1, 3, "B" => 2 );
// After the array is created, the default pointer is in the first element.
Echo current ($ arr )."
";
// Enter a forward position
Echo next ($ arr )."
";
// The default principle for sorting is from small to large.
Sort ($ arr );
// After sorting, the array pointer stops at the first element.
Echo current ($ arr )."
";
Echo next ($ arr )."
";
// Return a position
Echo prev ($ arr )."
";


Output:

1
3
1
2
1



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.