首页  >  文章  >  后端开发  >  这种情况FOREACH怎么用

这种情况FOREACH怎么用

WBOY
WBOY原创
2016-06-23 14:07:46930浏览

 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 有多少项,按匹配出的结果逐一替换,用完了就赋空值

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn