清除php空数组

时间:2010-05-21 23:14:30

标签: php arrays

我有以下数组,想要摆脱/删除空数组并按顺序重新排列。任何人都可以帮助我。


Array
(

    [ufile] => Array
        (

            [name] => Array
                (
                    [0] => chicken soup.jpg
                    [1] => 
                    [2] => hot n sour sup.jpg
                    [3] => 
                    [4] => 
                    [5] => 
                    [6] => 
                    [7] => 
                    [8] => 
                )

            [type] => Array
                (
                    [0] => 
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => 
                    [5] => 
                    [6] => 
                    [7] => 
                    [8] => 
                )

            [tmp_name] => Array
                (
                    [0] => 
                    [1] => 
                    [2] => 
                    [3] => 
                    [4] => 
                    [5] => 
                    [6] => 
                    [7] => 
                    [8] => 
                )

            [error] => Array
                (
                    [0] => 1
                    [1] => 4
                    [2] => 1
                    [3] => 4
                    [4] => 4
                    [5] => 4
                    [6] => 4
                    [7] => 4
                    [8] => 4
                )

            [size] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                    [4] => 0
                    [5] => 0
                    [6] => 0
                    [7] => 0
                    [8] => 0
                )

        )

)

4 个答案:

答案 0 :(得分:4)

我相信这会清理你的数组:删除空元素(那些评估为空的元素,例如“”和0等),删除重复的元素,然后对其进行排序。

$cleaned = array_map('array_filter', $array_to_be_cleaned);
$cleaned = array_map('array_unique', $cleaned);
$cleaned = array_map('sort', $cleaned);

答案 1 :(得分:4)

要过滤掉数组的空元素,请查看array_filter。 要对元素进行排序,请查看sort(或参阅此list of sorting functions以查看符合您需求的内容)

$newarray = array_filter($myarray);
sort($newarray);

这将获取您传递的数组(即$myarray),它将删除任何空值,然后将结果存储到$newarray。之后,sort会将剩余的值从最小到最大组织起来。

答案 2 :(得分:0)

试试array_filter($array)。它将删除所有NULL元素并返回已清除的数组。

答案 3 :(得分:0)

unset( $var['ufile']['type'] );
unset( $var['ufile']['tmp_name'] );

要排序,请使用sort()usort()或您想要的任何其他内容。这是一个good comparison of the different sort methods