我有一个数组如下所示

$fruit = array('  apple ','banana   ', ' , ',     '            cranberry ');

我想要一个数组,其中包含的值没有空白两侧,但它可以包含空值如何在php中做到这一点。输出数组应该是这样的

$fruit = array('apple','banana', ',', 'cranberry');

当前回答

Array_map ('trim', $data)将所有子数组转换为null。如果只需要为字符串修剪空格,而保持其他类型不变,你可以使用:

$data = array_map(
    function ($item) {
        return is_string($item) ? trim($item) : $item;
    },
    $data
);

其他回答

Array_map和trim可以完成这项工作

$trimmed_array = array_map('trim', $fruit);
print_r($trimmed_array);

如果你不想丢失一个关联数组的元素,不要使用array_walk或array_map!

一个略短版本的foreach解决方案:

foreach($array as &$val)
   $val = trim($val);

这适用于关联数组。

Multidimensional-proof解决方案:

array_walk_recursive($array, function(&$arrValue, $arrKey){ $arrValue = trim($arrValue);});
function trim_value(&$value) 
    { 
        $value = trim($value); 
    }
    
    
    // ut_sreco_dis Module
    public function disExcelUpload($file=""){
    
        ini_set('MAX_EXECUTION_TIME', -1);
        ini_set('memory_limit', '-1');   
    
        $file_upload    = $file;
    
        if (isset($file_upload) && !empty($file_upload)){ 
    
        //You can add directly the Composer Autoloder in your controller: 
        require FCPATH . 'vendor/autoload.php';
    
        try{   
            $objPHPExcel = PHPExcel_IOFactory::load($file_upload);   
        }   
        catch (Exception $e){
            die('Error loading file "' . pathinfo($file_upload, PATHINFO_BASENAME) . '": '.@$e->getMessage()); 
        }   
    
        $allDataInSheet         = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);  
        $sheet                  = $objPHPExcel->getSheet(0);  
        $highestRow             = $sheet->getHighestRow();  
        $highestCol             = $sheet->getHighestColumn();  
        $highestco              = $sheet->getHighestDataColumn();  
        $arrayCount             = count($allDataInSheet);   
        $now                    = date("Y-m-d H:i:s");      
        $flag = 0;
    
        $check_template = array(
            'A' => 'FIN_ID',
            'B' => 'SECCODE',
            'C' => 'SCHEME_NO',
            'D' => 'SEC_SCH',
            'E' => 'DISNO',
            'F' => 'DISQTY',
            'G' => 'BILLQTY',
            'H' => 'BILLREF',
            'I' => 'BILLDT',       
        );
    
            $input_template = $allDataInSheet[1];
    
            array_walk($input_template, $this->trim_value);
    
            $result = array_diff($check_template, $input_template);
    
            if(empty($result))
            {
                $this->srObject->truncTableDis();
    
                # loop for inserting data 
                for ($i = 2,$j=0; $i <= $highestRow; $i++){
    
                    $db_ch_ot = 64;
    
                    $fin_id         = trim($allDataInSheet[$i][chr(++$db_ch_ot)]);
                    $sec_code       = trim($allDataInSheet[$i][chr(++$db_ch_ot)]);
                    $sch_no         = trim($allDataInSheet[$i][chr(++$db_ch_ot)]);
                    $sec_sch        = trim($allDataInSheet[$i][chr(++$db_ch_ot)]);
                    $dis_no         = trim($allDataInSheet[$i][chr(++$db_ch_ot)]);
                    $dis_qty        = trim($allDataInSheet[$i][chr(++$db_ch_ot)]); 
                    $bill_qty       = trim($allDataInSheet[$i][chr(++$db_ch_ot)]);
                    $bill_ref       = trim($allDataInSheet[$i][chr(++$db_ch_ot)]);             
                    $bill_dt        = trim($allDataInSheet[$i][chr(++$db_ch_ot)]);
    
                    if(empty($bill_dt)){
                        $bill_dt = null;
                    }else{
                        $dip_dt = date("Y-m-d",strtotime($bill_dt));                    
                    }                
    
                    $dt             = date('Y-m-d H:i:s');
                    
                    $insert_data = array(  
                        "fin_id_data"           => $fin_id,
                        "sec_code_data"         => $sec_code,
                        "sch_no_data"           => $sch_no,
                        "sec_sch_data"          => $sec_sch,
                        "dis_no_data"           => $dis_no,                   
                        "dis_qty_data"          => $dis_qty,                   
                        "bill_qty_data"         => $bill_qty,
                        "bill_ref_data"         => $bill_ref,                   
                        "bill_dt_data"          => $bill_dt,                    
                        "created_at_data"       => $dt,
                        "updated_at_data"       => $dt,           
                    );
                    
                    if($this->srObject->insertSdisData($insert_data))
                    {
                        ++$flag;
                    }
    
    
                } //loop close  
            } else {
                $this->session->set_flashdata('error', 'Error. Invalid Excel Template');
                redirect(site_url('schill-bill-checking-suuti/sreco'));
            }  
    
            $this->session->set_flashdata('success', 'File Uploaded Successfully ..'.' New Record Inserted : '.$flag);
            redirect(site_url('schill-bill-checking-suuti/sreco'));
    
            
        }
        
    }
$fruit= array_map('trim', $fruit);