0
Primary_MathsJAN        
Primary_MathsFEB        
Primary_MathsMAR        
Primary_MathsAPR        
Primary_MathsMAY        
Primary_MathsJUNE       
Primary_MathsJULY       
Primary_MathsAUG        
Primary_MathsSEP        
Primary_MathsOCT        
Primary_MathsNOV        
Primary_MathsDEC

我在 foreach 数组之后设置了上面的键。在这里我想删除 JAN、FEB、MAR 等以及中间的 _(下划线)然后需要下面的结果

Primary Maths       
Primary Maths       
Primary Maths   
Primary Maths       
Primary Maths       
Primary Maths       
Primary Maths       
Primary Maths       
Primary Maths       
Primary Maths       
Primary Maths       
Primary Maths

有没有办法做到这一点?

4

2 回答 2

1

这应该给你你想要的

$array = array_map(
    function($val){ 
        return trim(str_replace(array("_","JAN","FEB","MAR","APR","MAY","JUNE","JULY", "AUG","SEP", "NOV","DEC"), " ", $val));
    }, 
    $array);

如果您想对数组数组执行此操作,您可以这样做:

$resultNew = array();

foreach($resultOld as $key=>$value)
{
    $resultNew[$key] = array_map(function($val){ return trim(str_replace(array("_","JAN","FEB","MAR","APR","MAY","JUNE","JULY", "AUG","SEP", "NOV","DEC"), " ", $val)); }, $value);
}

并且$resultNew会有你想要的值

于 2013-05-22T15:17:16.623 回答
0

我试过下面的情况

<?php
$input = array("Supriya mahajan", "green mahajan", "blue mahajan", "yellow Gupta");
$input1  = array_map(function($val){ 
                        return trim(str_replace(array("mahajan"), " ", $val));
                     }, $input );
//   echo implode("_ ", $input); 
//    echo implode("<br>",substr_replace( $input,' ',3,3));
echo $input1[0];
echo $input1[1];
echo $input1[2];
echo $input1[3];
?>  

如何一次性显示剩余阵列

于 2013-09-11T00:46:29.737 回答