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