php -str_replace无法遍历替换
见如下代码
$data = array('or','my','you','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);//无法替换??
// $data[$k] = str_replace(array_keys($arr),$arr,$v);
// $data[$k] = str_replace('l','k',$v);
}
}
var_dump($data);
?>
回复讨论(解决方案)
不知道楼主说的替换指的是不是把data里面每个字符串中出现过的字符都去arr里面去匹配下,有的换就替换。如果是这样的话 直接这样写就可以了:
<?php$data = array('or','my','you','which','love');$pattern = array('l','w','e','t','m');$arr = array('k','q','r','y',5);$newdata = str_replace($pattern, $arr, $data);var_dump($newdata);?>
$data[$k] = str_replace($pattern,$replacment,$v);
这句里面$v重新复制了,换成$data[$k]
$data[$k] = str_replace($pattern,$replacment,$data[$k]);
谢谢,我知道这样子写可以做到替换的效果,但是我的疑惑是为什么上面用foreach替换为什么不行呢?错在哪儿了呢。。
$data[$k] = str_replace($pattern,$replacment,$v);
$data[$k] 被循环的赋值,取最后一次赋值的结果.
谢谢,我知道这样子写可以做到替换的效果,但是我的疑惑是为什么上面用foreach替换为什么不行呢?错在哪儿了呢。。
你在循环中把$pattern $replacement $data[$k]都打印出来看看 应该就比较清晰了
0 orororororor1 mymymymymy5y2 youyouyouyouyouyou3 whichwhichqhichwhichwhichwhich4 lovekovelovelovrlovelove
楼主这是我打印出来的循环结果,其实在foreach中。str_replace已经生效,但是被后面的替换了,所以你才觉得没有替换
foreach ($data as $k => $v) {
中的 $v 是 $data[$k] 的副本
在内层循环中
$data[$k] = str_replace($pattern,$replacment, $v);
$v 并没有改变,所以所有的替换都是对原始数据,而不是对上次替换结果的
如果你写作
foreach ($data as $k => &$v) {
就可以看到,的确是起作用了

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.
