2013-08-05 53 views
0

更換空的空間,我有一個數組,看起來像這樣:在多維數組文本

Array 
(
    [0] => Array 
     (
      [id] => 1 
      [firstname] => Firstname one 
      [lastname] => Lastname one 
     ) 

    [1] => Array 
     (
      [id] => 2 
      [firstname] => Firstname two 
      [lastname] => Lastname two 
     ) 

    [2] => Array 
     (
      [id] => 3 
      [firstname] => Firstname three 
      [lastname] => Lastname three 
     ) 
) 

我想通過陣列走路和更換每一個空白或空的東西,如N/A

我嘗試下面的代碼,但沒有成功:

foreach($members as $member) 
{ 
    foreach($member as $k => $v) 
    { 
     if($member[$k][$v] == null || trim($member[$k][$v]) == "") 
     { 
       $member[$k][$v] = "N/A"; 
     } 
    } 
} 
+0

@KristerAndersson這是一個有問題的拼寫錯誤 –

回答

1

我認爲這是你的目標:

$members = array(0 => array('id' => ""), 1 => array('id' => "hello"), 2 => array('name' => "")); 

foreach($members as &$member) 
{ 
    foreach($member as $k => $v) 
    { 
     if (empty($v)) { 
      $member[$k] = "N/A"; 
     } 
    } 
} 

var_dump($members); 
0
You can use array_push function http://php.net/manual/en/function.array-push.php 
foreach($members as $member) 
    { 
    foreach($member as $k => $v) 
    { 
     if($member[$k][$v] == null || trim($member[$k][$v]) == "") 
     { 
      array_push($member,'N/A'); 
     } 
    } 
    }