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 之类的东西替换每个空白空间或 null

我尝试了下面的代码但没有成功:

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

2 回答 2

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);
于 2013-08-06T00:03:44.647 回答
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');
        }
     }
  }
于 2013-08-06T00:02:40.610 回答