PHP平面数组一维高且空值

时间:2014-10-22 19:48:21

标签: php arrays flat

我有这个数组$data

Array
(
  [0] => Array
      (
          [0] => 1
      )

  [1] => Array
      (
      )

  [2] => Array
      (
      )

  [3] => Array
      (
          [0] => test@email.net
      )

)

每个数组中始终只有一个元素。那么,我如何将$data数组转换为此?我也需要空值:

Array
(
  [0] => 1
  [1] =>
  [2] =>
  [3] => test@email.net
)

1 个答案:

答案 0 :(得分:1)

使用array_map

$arr = Array
(
  [0] => Array
      (
          [0] => 1
      )

  [1] => Array
      (
      )

  [2] => Array
      (
      )

  [3] => Array
      (
          [0] => test@email.net
      )

);

function flaten($n)
{
    if (isset($n[0])) {
       return $n[0];
    } else {
       return "";
    }
}

$resultArray = array_map(flaten, $arr);