foreach($arr as $value){ $value=preg_replace("/href=\"\//i",'href="'.$link,$value); echo $value."
";//此处$value为替换之后的值。 } echo $arr[1];//此处显示的仍为替换前$arr[1]的值。//请问这个是怎么回事呢?``
Reply content:
foreach($arr as $value){ $value=preg_replace("/href=\"\//i",'href="'.$link,$value); echo $value."
";//此处$value为替换之后的值。 } echo $arr[1];//此处显示的仍为替换前$arr[1]的值。//请问这个是怎么回事呢?``
- 38 minutes ago Question
- Comments
- Invitation to answer
Please login and comment first
Sort By default sorting time
4 answers
The answer is helpful to the person, has the reference value 0 The answer is no help, it's the wrong answer, irrelevantly replying .
foreach($arr as &$value){ $value=preg_replace("/href=\"\//i",'href="'.$link,$value); echo $value."
";//此处$value为替换之后的值。}echo $arr[1];//此处显示的仍为替换前$arr[1]的值。
Because of the scope of the variable, if you modify the value inside, and you need to save the result, you need to define the type as a reference type , that is &
.
Change it.
foreach($arr as $key=>$value){ $arr[$key]=preg_replace("/href=\"\//i",'href="'.$link,$value); echo $arr[$key]."
";//此处$value为替换之后的值。 }
You just did a replace operation on the local variable $value in the loop, 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."
";//此处$value为替换之后的值。}echo $arr[1];//此处显示的仍为替换前$arr[1]的值。
Get it straight, pass it by value, and pass it by reference.