Home > Backend Development > PHP Tutorial > PHP 5.2.x array operation details sharing

PHP 5.2.x array operation details sharing

WBOY
Release: 2016-07-25 08:55:41
Original
1044 people have browsed it
  1. $arr = array("a" => 1,"b" => 2,"c" => 3);
Copy code

If Defining an array like this will result in a compilation error:

  1. $arr = array("a" = 1,"b" = 2,"c" = 3);
Copy code

Therefore, when defining the array Can only be used => 2,

  1. $arr = array("a" => 1,"b" => 2,"c" => 3);
  2. echo $arr[0];
  3. echo $arr[1];
Copy the code

The result will be blank.

Correct printing method:

  1. echo $arr["a"];
Copy code

3. When adding elements or modifying elements, you can only use =, not =>

  1. $arr = array("a" => 1,"b" => 2,"c" => 3);
  2. $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:

  1. $arr = array("a" => 1,"b" => 2,"c" => 3);
  2. $arr["d"] = 4;
  3. $arr["c"] = 6;
Copy code

To delete elements, use unset:

  1. unset ($arr["c"]);
Copy code

4. Think about the following code, what will be output?

  1. $arr = array("a" => 1,2,"b" => 3,4);
  2. $arr[] = 5;
  3. foreach($arr as $key => $value)
  4. {
  5. echo "key:$key value:$value
    ";
  6. }
Copy the code

Output result:

  1. $arr = array("a" => 1,3,"b" => 2);
  2. //After creating the array, the default pointer points to the first element
  3. echo current($arr)."
    ";
  4. //Go forward one position
  5. echo next($arr)."
    ";
  6. //The default principle of sorting is from small to large
  7. sort( $arr);
  8. //After finishing, the array pointer stops at the first element
  9. echo current($arr)."
    ";
  10. echo next($arr)."
    ";
  11. //Go back one position
  12. echo prev($arr)."
    ";
Copy code

Output result: 1 3 1 2 1



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template