Php uses foreach to traverse the array cyclically. The execution in the loop is successful, but it is not successful outside the loop? -SegmentFault

Source: Internet
Author: User
Tags variable scope
{Code ...}
Foreach ($ arr as $ value) {$ value = preg_replace ("/href = \" \ // I ", 'href = "'. $ link, $ value); echo $ value."
"; // Here $ value is the value after replacement. } Echo $ arr [1]; // The value displayed here is still the value of $ arr [1] before replacement. // What is this? ''

Reply content:

Foreach ($ arr as $ value) {$ value = preg_replace ("/href = \" \ // I ", 'href = "'. $ link, $ value); echo $ value."
"; // Here $ value is the value after replacement. } Echo $ arr [1]; // The value displayed here is still the value of $ arr [1] before replacement. // What is this? ''

  • Ask a question 38 minutes ago
  • Comment
  • Reply

Please log on first and then comment

Sort by default

4 answers

The answer is helpful to people and has reference value.0The answer is not helpful. It is a wrong answer.

Foreach ($ arr as & $ value) {$ value = preg_replace ("/href = \" \ // I ", 'href = "'. $ link, $ value); echo $ value."
"; // Here $ value is the value after replacement .} Echo $ arr [1]; // The value displayed here is still the value of $ arr [1] before replacement.

If you modify the value in the variable scope and save the result, you need to define the typeReference Type, That is&.

Change to this

Foreach ($ arr as $ key => $ value) {$ arr [$ key] = preg_replace ("/href = \" \ // I ", 'href = "'. $ link, $ value); echo $ arr [$ key]."
"; // Here $ value is the value after replacement. }

You just performed a replace operation on the local variable $ value in the loop body, and did not return the value to $ arr.

Foreach ($ arr as $ key => $ value) {$ value = preg_replace ("/href = \" \ // I ", 'href = "'. $ link, $ value); $ arr [$ key] = $ value; echo $ value."
"; // Here $ value is the value after replacement .} Echo $ arr [1]; // The value displayed here is still the value of $ arr [1] before replacement.

First, let's figure out how to pass by value and by reference.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.