A detailed example of PHP 5.2.x array operation

Source: Internet
Author: User
Array manipulation in PHP 5.2.x

Just read the PHP Getting Started tutorial, summarize some of the PHP array operation caused by different PHP versions, some problems.

The following content is tested in the php5.2.5 environment.
1.

<?php$arr = Array ("a" = = 1, "b" = = 2, "c" = 3);

If this defines an array, it will report a compilation error:

Copy Code code example:

<?php$arr = Array ("A" = 1, "b" = 2, "c" = 3);


Therefore, when you define an array, you can only use the =

Copy Code code example:

<?php$arr = Array ("a" = = 1, "b" = 2, "c" = 3); echo $arr [0];echo $arr [1];


It was a blank shot.

The correct way to print:

Copy Code code example:

echo $arr ["a"];

3, add elements or modify the elements can only use =, can not use =

Copy Code code example:

<?php$arr = Array ("a" = = 1, "b" = 2, "c" = 3); $arr ["c"] = 6;


The above operation method, in PHP 5.2.5 will appear the compilation error

Add elements or modify elements to write this:

Copy Code code example:

<?php$arr = Array ("a" = = 1, "b" = 2, "c" = 3); $arr ["D"] = 4; $arr ["c"] = 6;

To delete an element, use unset:

Copy Code code example:

unset ($arr ["C"]);

4, think of the following code, what will output?

Copy Code code example:

<?php$arr = Array ("a" = =, "b" = 3,4); $arr [] = 5;foreach ($arr as $key + $value) {    echo "key: $key value: $value <br> ";}


Output Result:

Key:a value:1key:0 value:2key:b value:3key:1 value:4key:2 value:5

Note: PHP automatically uses a 0-based number as the key only if the user does not have a key defined.

5, the array in PHP is a pointer, you can forward and backward operation of the group

Copy Code code example:

<?php$arr = Array ("a" = = 1,3, "b" = 2);

After the array is created, the default pointer refers to the first element
Echo current ($arr). " <br> ";
Move forward a position
Echo Next ($arr). " <br> ";
The default rule for grooming is small to large
Sort ($arr);
After finishing the array pointer again stops at the first element
Echo current ($arr). " <br> ";
Echo Next ($arr). " <br> ";
Back one position
Echo prev ($arr). " <br> ";

Output Result:

13121

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.