用str_replace替换数组时有点怪

WBOY
Release: 2016-06-23 14:27:40
Original
881 people have browsed it

$str = '2abc';$arr1 = array('0', '1', '2', '3', '4', '5', '6');$arr2 = array('9', '8', '7', '6', '5', '4', '3');$str = str_replace($arr1, $arr2, $str);echo $str;//输出结果正常:7abc
Copy after login

$str = '2abc';$arr1 = array('0', '1', '2', '3', '4', '5', '6', '7');$arr2 = array('9', '8', '7', '6', '5', '4', '3', 'W');$str = str_replace($arr1, $arr2, $str);echo $str;//输出结果有误:Wabc
Copy after login

请问为什么会这样啊?
谢谢!


回复讨论(解决方案)

'7'
'w'

后边不是有这个吗,第一次2->7,第二次 7->w

'7'
'w'

后边不是有这个吗,第一次2->7,第二次 7->w

是分开测试上面这两段代码的,它们并不在同一文件中。

我知道,我说的就是第二个。

$str = '2abc';
$arr1 = array('0', '1', '2', '3', '4', '5', '6', '7');
$arr2 = array('9', '8', '7', '6', '5', '4', '3', 'W');
$str = str_replace($arr1, $arr2, $str);
echo $str;
//输出结果有误:Wabc

他的意思第一次查找$arr1的时候把2替换成了7 所以$str=7abc 然后继续查找$arr1的时候又把7替换成了W 所以就成了Wabc

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!