在PHP中删除某些内部值的数组数组

时间:2017-12-04 08:40:32

标签: php arrays

拥有这种阵列:

Array (
   [0] => Array (
        [0] => Array (
              [title] => "Test string"
              [lat] => "40.4211"
              [long] => "-3.70118"
              )
         )
   [1] => Array (
            [0] => Array (
                  [title] => "Test string 2"
                  [lat] => "10.0"
                  [long] => "-23.0"
                  )
            [1] => Array (
                  [title] => "Test string 3"
                  [lat] => "10.0"
                  [long] => "-23.0"
                  )
          )
   [2] => Array (
        [0] => Array (
              [title] => "Test string 6"
              [lat] => "11.1"
              [long] => "7.7"
              )
         )
)

如何为所有长度为1的内部数组摆脱数组

我想要的输出是:

Array (
   [0] => Array (
          [title] => "Test string"
          [lat] => "40.4211"
          [long] => "-3.70118"
   )
   [1] => Array (
            [0] => Array (
                  [title] => "Test string 2"
                  [lat] => "10.0"
                  [long] => "-23.0"
                  )
            [1] => Array (
                  [title] => "Test string 3"
                  [lat] => "10.0"
                  [long] => "-23.0"
                  )
    )
    [2] => Array (
          [title] => "Test string 6"
          [lat] => "11.1"
          [long] => "7.7"
     )
)

提前致谢。

PS:我使用的是PHP 5.3

3 个答案:

答案 0 :(得分:3)

你可以这样做: -

foreach($array as $key=>$value){
  if(is_array($value) && count($value) ==1){
    $array[$key] = $value[0];
  }
}

输出: - https://eval.in/912263

或者您也可以使用 Passing by Reference 机制: -

foreach($array as &$value){
  if(is_array($value) && count($value) ==1){
    $value = $value[0];
  }
}

输出: - https://eval.in/912264

参考: - Passing by Reference

答案 1 :(得分:2)

转换每个值,如果长度为1则返回其第一个子节点,否则返回整个值:

ms_div( C, mid_i );

答案 2 :(得分:1)

假设2D数组保持原样

FireBase