2

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
                )

        )

)
2
  • What do you mean by 'rearrange it in an order'? Commented May 21, 2010 at 23:16
  • rearrange the key order example rearrange key to become 0 and 1 for the example above which was 0 and 2 before that Commented May 21, 2010 at 23:18

4 Answers 4

4

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);
Sign up to request clarification or add additional context in comments.

1 Comment

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.
4

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.

1 Comment

@Gumbo Good call, I don't know how I missed that! :S
0
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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.