Php-str_replace cannot traverse replace

Source: Internet
Author: User
See the following code
$data = Array (' or ', ' my ', ' I ', ' which ', ' love ');
$arr = Array (' l ' = = ' K ', ' w ' = = ' Q ', ' e ' = = ' R ', ' t ' = ' y ', ' m ' =>5);
foreach ($data as $k = = $v) {
foreach ($arr as $key = = $value) {
$pattern = $key;
$replacment = $value;
$data [$k] = Str_replace ($pattern, $replacment, $v);//cannot be replaced??
$data [$k] = Str_replace (Array_keys ($arr), $arr, $v);
$data [$k] = Str_replace (' l ', ' K ', $v);
}
}

Var_dump ($data);
?>


Reply to discussion (solution)

Do not know the landlord said the replacement refers to the data inside each string appears in the characters are to arr inside to match, and some replacement. If that's the case, just write it this way:

 
  

$data [$k] = Str_replace ($pattern, $replacment, $v);
This sentence inside $v re-copied, replaced by $data[$k]
$data [$k] = Str_replace ($pattern, $replacment, $data [$k]);

Thank you, I know it can be replaced by this kind of writing, but my question is why can't I replace it with a foreach? Where's the mistake?

$data [$k] = Str_replace ($pattern, $replacment, $v);
$data [$k] is the value of the loop, taking the result of the last assignment.

Thank you, I know it can be replaced by this kind of writing, but my question is why can't I replace it with a foreach? Where's the mistake?


You print the $pattern $replacement $data [$k] in the loop to see if it's clearer.

0 orororororor1 mymymymymy5y2 youyouyouyouyouyou3 whichwhichqhichwhichwhichwhich4 lovekovelovelovrlovelove


Landlord this is my printed out of the loop results, in fact, in foreach. Str_replace has come into effect, but is replaced by the back, so you feel no replacement

foreach ($data as $k = = $v) {
The $v in is a copy of the $data [$k]
In the inner loop
$data [$k] = Str_replace ($pattern, $replacment, $v);
$v does not change, so all substitutions are to the original data, not to the last substitution result

If you write
foreach ($data as $k = & $v) {
As you can see, it does work.

  • 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.