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