从数组内的每个字符串中删除部分字符串

时间:2013-05-22 15:12:41

标签: php arrays multidimensional-array rename

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 the array之后我有上面的键设置。在这里键列表我要删除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

有没有这样做?

2 个答案:

答案 0 :(得分: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会有你想要的值

答案 1 :(得分: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];
?>  

如何一次显示剩余阵列