这种情况FOREACH怎么用

WBOY
發布: 2016-06-23 14:07:46
原創
937 人瀏覽過

 foreach($str as $key=>$row)	     {		   if(preg_match_all('/\b[Nn][Ss][Kk]-\w{5}/',$sn,$match)){	       $str[$key]['darfon_pn']=substr_replace($match[0][0],$row['darfon_pn'],7,2);		   }else{		   $str[$key]['darfon_pn'] = '';		   }         }
登入後複製

以上代码只遍历了$match[0][0],如何才能遍历整个$match每个值


回复讨论(解决方案)

$str[$key]['darfon_pn']=substr_replace($match[$key][0],$row['darfon_pn'],7,2);

$str[$key]['darfon_pn']=substr_replace($match[0][ $key],$row['darfon_pn'],7,2);

if(preg_match_all('/\b[Nn][Ss][Kk]-\w{5}/',$sn,$match)) { //由于$sn是另外的变量,没有必要放在循环里多次检查  foreach($match[0] as $v) { //对于每一个匹配到的串    foreach($str as $key=>$row) { //修改$str      $str[$key]['darfon_pn'] = substr_replace($v, $row['darfon_pn'],7,2);    }  }}else{ //否则  foreach($str as $key=>$row) {    $str[$key]['darfon_pn'] = ''; //赋空值  }}
登入後複製
登入後複製

if(preg_match_all('/\b[Nn][Ss][Kk]-\w{5}/',$sn,$match)) { //由于$sn是另外的变量,没有必要放在循环里多次检查  foreach($match[0] as $v) { //对于每一个匹配到的串    foreach($str as $key=>$row) { //修改$str      $str[$key]['darfon_pn'] = substr_replace($v, $row['darfon_pn'],7,2);    }  }}else{ //否则  foreach($str as $key=>$row) {    $str[$key]['darfon_pn'] = ''; //赋空值  }}
登入後複製
登入後複製

这样基本上是对的了,只是为何输出只替换了第一个循环到的字符串,其他都是删除了,例:NSK-34582 NSK-UUS89 NSK-89234,输出为NSK-892NSK-UUSNSK-345PP,我想要的是NSK-892PPNSK-UUSPPNSK-345PP

最初的 $str[$key]['darfon_pn'] 的内容是什么?
测试代码

$sn = 'NSK-34582 NSK-UUS89 NSK-89234';preg_match_all('/\b[Nn][Ss][Kk]-\w{5}/',$sn,$match);print_r($match[0]);$str = '________';foreach($match[0] as $v) {  $str = substr_replace($v, $str,7,2);  echo $str,PHP_EOL;}
登入後複製
登入後複製
登入後複製
Array
(
[0] => NSK-34582
[1] => NSK-UUS89
[2] => NSK-89234
)
NSK-345________
NSK-UUSNSK-345________
NSK-892NSK-UUSNSK-345________

是你期望的吗?

最初的 $str[$key]['darfon_pn'] 的内容是什么?
测试代码

$sn = 'NSK-34582 NSK-UUS89 NSK-89234';preg_match_all('/\b[Nn][Ss][Kk]-\w{5}/',$sn,$match);print_r($match[0]);$str = '________';foreach($match[0] as $v) {  $str = substr_replace($v, $str,7,2);  echo $str,PHP_EOL;}
登入後複製
登入後複製
登入後複製
Array
(
[0] => NSK-34582
[1] => NSK-UUS89
[2] => NSK-89234
)
NSK-345________
NSK-UUSNSK-345________
NSK-892NSK-UUSNSK-345________

是你期望的吗?
NSK-345________
NSK-UUS________
NSK-892________
这个是我期望的


最初的 $str[$key]['darfon_pn'] 的内容是什么?
测试代码

$sn = 'NSK-34582 NSK-UUS89 NSK-89234';preg_match_all('/\b[Nn][Ss][Kk]-\w{5}/',$sn,$match);print_r($match[0]);$str = '________';foreach($match[0] as $v) {  $str = substr_replace($v, $str,7,2);  echo $str,PHP_EOL;}
登入後複製
登入後複製
登入後複製
Array
(
[0] => NSK-34582
[1] => NSK-UUS89
[2] => NSK-89234
)
NSK-345________
NSK-UUSNSK-345________
NSK-892NSK-UUSNSK-345________

是你期望的吗?
NSK-345________
NSK-UUS________
NSK-892________
这个是我期望的


这样呢?

function actionTest(){

$sn = 'NSK-34582 NSK-UUS89 NSK-89234';
preg_match_all('/\b[Nn][Ss][Kk]-\w{5}/',$sn,$match);
print_r($match[0]);
$str = '________';
foreach($match[0] as $v) {
$key = substr_replace($v, $str,7);
echo $key;
}

}

明白你的意思了

preg_match_all('/\b[Nn][Ss][Kk]-\w{5}/',$sn,$match);foreach($str as $key=>$row){  if($match[0][0]){    $str[$key]['darfon_pn']=substr_replace($match[0][0],$row['darfon_pn'],7,2);    array_shift($match[0]);  }else{    $str[$key]['darfon_pn'] = '';  }}
登入後複製
无论 $str 有多少项,按匹配出的结果逐一替换,用完了就赋空值

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板