开发者

clear php empty array

i have the following array and want to get rid/remove the empty array and rearrange it in an order.can anyone help me please.


Array
(

    [ufile] => Array
        (

            [name] => Array
                (
                    [0] => chicken soup.jpg
                    [1] => 
                    [2] => hot n sour sup.jpg
                    [3] => 
                    [4] => 
     开发者_JS百科               [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
                )

        )

)


I believe this will cleanup your array: remove empty elements (those that evaluate to empty, eg "" and 0 equally), remove duplicate elements, and sort it.

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


To filter out the empty elements of the array, check out array_filter. To sort the elements, check out sort (or refer to this list of sorting functions to see what meets your needs)

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

This will take the array you pass it (ie. $myarray) and it will strip out any empty values, then it will store the results to $newarray. After that, sort will organize the remaining values from least to greatest.


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

To sort, use sort(), usort(), or any other you want. Here's a good comparison of the different sort methods.


Try array_filter($array). It will remove all NULL elements and return the cleared array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜