-
- $arr = array("a" => 1,"b" => 2,"c" => 3);
-
Copy code
If Defining an array like this will result in a compilation error:
-
-
- $arr = array("a" = 1,"b" = 2,"c" = 3);
-
Copy code
Therefore, when defining the array Can only be used =>
2,
-
- $arr = array("a" => 1,"b" => 2,"c" => 3);
- echo $arr[0];
- echo $arr[1];
-
Copy the code
The result will be blank.
Correct printing method:
3. When adding elements or modifying elements, you can only use =, not =>
-
- $arr = array("a" => 1,"b" => 2,"c" => 3);
- $arr["c"] = > 6;
-
Copy the code
The above operation method will cause a compilation error in php 5.2.5
To add elements or modify elements, write like this:
-
- $arr = array("a" => 1,"b" => 2,"c" => 3);
- $arr["d"] = 4;
- $arr["c"] = 6;
Copy code
To delete elements, use unset:
4. Think about the following code, what will be output?
-
- $arr = array("a" => 1,2,"b" => 3,4);
- $arr[] = 5;
- foreach($arr as $key => $value)
- {
- echo "key:$key value:$value
";
- }
-
Copy the code
Output result:
-
- $arr = array("a" => 1,3,"b" => 2);
- //After creating the array, the default pointer points to the first element
- echo current($arr)."
";
- //Go forward one position
- echo next($arr)."
";
- //The default principle of sorting is from small to large
- sort( $arr);
- //After finishing, the array pointer stops at the first element
- echo current($arr)."
";
- echo next($arr)."
";
- //Go back one position
- echo prev($arr)."
";
Copy code
Output result:
1
3
1
2
1
|