PHP 5.2.x details not to be noticed: array pieces

Source: Internet
Author: User
Tags php tutorial

Just read the PHP tutorial, due to the PHP version of the problem found that there is a place to study the array here
Several experiments were made for php5.2.5:

1,



$arr   =   Array ( " a "   =>   1 , " b "   =>   2 , " C "   =>   3 );

However, if you define an array like this, you will report a compilation error:



$arr   =   Array ( " a "   =   1 , " b "   =   2 , " C "   =   3 );

So when you define an array, you can only use =>

2,



$arr   =   Array ( " a "   =>   1 , " b "   =>   2 , " C "   =>   3 );
Echo   $arr [ 0 ];
Echo   $arr [ 1 ];

It is a blank, so that it can be played out:



Echo   $arr [ " a " ];


3, add elements or modify elements of the time can only be used =, can not use =>



$arr   =   Array ( " a "   =>   1 , " b "   =>   2 , " C "   =>   3 );
$arr [ " C " ]  =>   6 ;


Like this may be used in previous releases, but there will be a compilation error when 5.2.5
Add elements or modify elements to write as follows:



$arr   =   Array ( " a "   =>   1 , " b "   =>   2 , " C "   =>   3 );
$arr [ " D " ]  =   4 ;
$arr [ " C " ]  =   6 ;
Delete element to use unset


unset  ( $arr [ " C " ]);

4. Do 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 <br> " ;
}

Results:

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 will automatically use a number starting with 0 as a key when the user does not have a key defined.

5, the array in PHP has a pointer, you can set the group forward and backward operation



$arr   =   Array ( " a "   =>   1 , 3 , " b "   =>   2 );
// After you create the array, the default pointer refers to the first element
Echo   Current ( $arr ) . " <br> " ;
// forward one position
Echo   Next ( $arr ) . " <br> " ;
// The default principle of collation is from small to large
Sort ( $arr );
// after finishing, the array pointer stops at the first element
Echo   Current ( $arr ) . " <br> " ;
Echo   Next ( $arr ) . " <br> " ;
// back one position
Echo   prev ( $arr ) . " <br> " ;


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.