见如下代码
$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) {
就可以看到,的确是起作用了

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

SublimeText3 English version
Recommended: Win version, supports code prompts!

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