tags:

views:

217

answers:

4

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] => 
                    [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
                )

        )

)
A: 

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

Simon
A: 
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.

Seb
+3  A: 

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);
mr.b
Also, it is obvious (to me) that you have intention to clean up $_FILES array from multiple file upload form with <input type="file" name="ufile[]" ... />. Better approach would be to loop through it and check each upload manually: for ($i = 0; $i < count($_FILES['ufile']['name']); $i ++ ) { $error = $_FILES['ufile']['error'][$i]; ... }, check error code and act accordingly.
mr.b
+2  A: 

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.

Dominic Barnes
`sort` is a mutator function and does only accept variables.
Gumbo
@Gumbo Good call, I don't know how I missed that! :S
Dominic Barnes