2015-03-25 231 views
0

我想在這裏解析。這裏是刪除陣列中的元素PHP

Array ([61] => [128] => [158] => [224] => [281] => AGAS, [282] => JAMES [283] => JAY [285] => [329] => [330] => 03/02/2015 [331] => 6:46 [332] => am [333] => 5:42 [334] => pm [433] => APOLLOL, [434] => KARMIN [436] => [480] => [481] => 03/01/2015 [598] =>) 

,我可以看到有我的陣列空值,我已經試過這個代碼我的數組的內容:

$final2=array_filter($final); 
$trimmed_array=array_map('trim',$final2); 

但一些空值都沒有刪除,也我怎樣才能使我的陣列像這樣:

Array ([1] => AGAS, [2] => JAMES [3] => JAY [4] => [5] => 03/02/2015 [6] => 6:46 [7] => am [8] => 5:42 [9] => pm [10] => APOLLOL, [11] => KARMIN [12] => 03/01/2015) 
+0

@ hek2mgl感謝您的編輯 – user3797088 2015-03-25 03:33:18

+0

您可以安裝'xdebug'擴展名並重新生成var_dumps嗎?它將顯示數據類型並在字符串周圍放置'「」。 – hek2mgl 2015-03-25 03:41:14

回答

0

請看看下面給出的函數。希望這會幫助你。該函數將所有空值轉換爲空白(「」)。

function array_remove_empty($Yourarray) { 
     foreach ($Yourarray as $key => $value) { 
      if (is_array($value)) { 
       $Yourarray[$key] = array_remove_empty($Yourarray[$key]); 
      } 

      if (empty($Yourarray[$key])) { 
       $Yourarray[$key] = ""; 
      } 
     } 

     return $Yourarray; 
    } 
0

$inputArray = array (
'61'=> '', 
'128' => '', 
'158' => '', 
'224' => '', 
'281' => 'AGAS', 
'282' => 'JAMES' , 
'283' => 'JAY' , 
'285' =>'', 
'329' => '', 
'330' => '03/02/2015', 
'331' => '6:46' , 
'332' => 'am' , 
'333' => '5:42', 
'334' =>'pm' , 
'433' => 'APOLLOL', 
'434' =>'KARMIN' , 
'436' =>'' , 
'480' => '', 
'481' => '03/01/2015' , 
'598' => '', 
); 

$reqResult = array (
'1' => 'AGAS', 
'2' => 'JAMES', 
'3' => 'JAY', 
'4' =>'', 
'5' => '03/02/2015', 
'6' => '6:46', 
'7' => 'am' , 
'8' => '5:42' , 
'9' => 'pm' , 
'10' => 'APOLLOL', 
'11'=> 'KARMIN' , 
'12' => '03/01/2015', 
); 

在reqResult林假設偏移4是一個錯誤。

$myResult = array(); 
$i=1; 
foreach($inputArray as $key => $value){ 
if($value!=''){ 
    $myResult[$i]= $value; 
    $i++; 
    } 
} 
print_r($myResult);