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