Home > Backend Development > PHP Tutorial > PHP extract array split into multiple variables

PHP extract array split into multiple variables

WBOY
Release: 2016-07-25 08:54:05
Original
1275 people have browsed it
  1. $size = "old size"; //Pay attention to the value of the last size variable.
  2. $a = array(
  3. "color" => "red",
  4. "size" => "XXL",
  5. "price" => "53");
  6. extract($a);
  7. echo " color = $color
    ";
  8. echo "size = $size
    ";
  9. echo "price = $price
    ";
  10. ?>
Copy code

Output result: color=red size=XXL price = 53

Through the above example, we found that the value of $size is XXL, not the previous "old size", which means that by default, when the key in the array conflicts with an existing variable, the original variable will be overwritten.

Continue to introduce the last two optional parameters of the extract function.

The second parameter $extract_type is used to control the processing method when a conflict occurs. Possible values ​​are:

  1. $a = array(
  2. "color" => "red",
  3. "size" => "XXL",
  4. "price" => "53");
  5. extract($a,EXTR_PREFIX_ALL,"SC");
  6. echo "color = $SC_color
    ";
  7. echo "size = $SC_size
    ";
  8. echo "price = $SC_price
    ";
  9. extract($a,EXTR_REFS);
  10. $color="green";
  11. echo $a['color']; //View the value of the original array
  12. ?>
Copy code

Output result: color=red size=XXL price = 53 green



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