douti6740 2017-07-18 12:42
浏览 59
已采纳

消除空值嵌套数组php

I have an array and contents nested inside the original array. The contents of the array look like-

$myArray

[0] => Array(
[ID] => 1
[Fruit] => Apple
[State] => Ohio
[description]
   Array(
     [0] => This is sample description
     [1] => This is sample description 2
     [2] => 
     [3] => 
     [4] => 


)
 [price]
   Array(
     [0] => 20
     [1] => 15
     [2] => 
     [3] => 
     [4] => 
)
[1] => Array(
[ID] => 1
[Fruit] => Apple
[State] => Ohio
[description]
   Array(
     [0] => This is sample description
     [1] => This is sample description 2
     [2] => 
     [3] => 
     [4] => 
)
[price]
   Array(
     [0] => 20
     [1] => 15
     [2] => 
     [3] => 
     [4] => 
)

I want to get rid of the null values in the nested array. When i use the following:

$newArray = array();
foreach ($firstArray as $row){
    if ($row !== null)
        $newArray[] = $row;

}
echo $newArray;

The new array does not get rid of the null values in the array.

  • 写回答

1条回答 默认 最新

  • doudandui1592 2017-07-18 12:44
    关注

    You can do it like below:-

    function array_filter_to_each_sub_array_recursively($input){
        foreach ($input as &$value){
            if (is_array($value)){
                $value = array_filter_to_each_sub_array_recursively($value);
            }
        }
        return array_filter($input);
    }
    
    $myArray = array_filter_to_each_sub_array_recursively($myArray);
    
    print_r($myArray);
    

    Output:-https://eval.in/833982

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?