Php 用其他preg_替换的值替换数组值
我有一个数组,我知道它的值是来自某处的JPGs 我需要转到返回到该数组的每个值,并替换一些字符 然后将返回值的值设置为其他值 这是代码,这是我试过的Php 用其他preg_替换的值替换数组值,php,arrays,preg-replace,Php,Arrays,Preg Replace,我有一个数组,我知道它的值是来自某处的JPGs 我需要转到返回到该数组的每个值,并替换一些字符 然后将返回值的值设置为其他值 这是代码,这是我试过的 //first piece of code $data['images'] = array(); foreach ($single['PictureURL'] as $ispec) { $data['images'][] = $ispec; $ispec = preg_replace
//first piece of code
$data['images'] = array();
foreach ($single['PictureURL'] as $ispec) {
$data['images'][] = $ispec;
$ispec = preg_replace('/\$_[0-9]+.JPG\b/i', '$_10.JPG', $ispec);
$file = 'C:\wamp64\www\mz\images1.txt';
file_put_contents ($file, $ispec, FILE_APPEND);
//images1.txt shows all images returned fine with modified strings
}
//second piece of code
$product->imageUrl = $data['images'][0];
unset($data['images'][0]);
$product->subImageUrl = $data['images'];
$file = 'C:\wamp64\www\mz\images3.txt';
file_put_contents ($file, $data['images'], FILE_APPEND);
//images3.txt shows all the images returned but without being modified?? WHY??!
第一段代码对所有值都有效,替换也很好
第二段代码是我的问题,它返回旧的未修改图像的值,而我没有
我需要在写入之前修改图像
“$product->imageUrl&$product->subImageUrl”问题很简单。您正在修改您的数据 将其存储在
$data['images']
中。要解决此问题,只需将此行移动到
在preg\u更换后
:
foreach ($single['PictureURL'] as $ispec) {
$ispec = preg_replace('/\$_[0-9]+.JPG\b/i', '$_10.JPG', $ispec);
$data['images'][] = $ispec;
$file = 'C:\wamp64\www\mz\images1.txt';
file_put_contents ($file, $ispec, FILE_APPEND);
}
谢谢,伙计,我正准备开始打破东西:D