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

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!