Search code examples
phparrays

Remove an item from an array then re-insert a value in the same position with the same key


I have an file uploading site, it has an option of uploading through urls, what I am trying to do is whenever a user uploads through url, I check my database if a file exists that was uploaded through same url it displays the download url directly instead of uploading it again.

The data sent to uploading script is in array form like:

Array ( 
[0] => http://i41.tinypic.com/3342r93.jpg 
[1] => http://i41.tinypic.com/28cfub7.jpg 
[2] => http://i41.tinypic.com/12dsa32.jpg 
)

and the array used for outputing the results is in form like this:

Array
(
    [0] => Array
        (
            [id] => 43
            [name] => 3342r93.jpg
            [size] => 362750
            [descr] => 
            [password] => 
            [delete_id] => 75CE
            [upload_id] => 75F45CAE1
        )

    [1] => Array
        (
            [id] => 44
            [name] => 28cfub7.jpg
            [size] => 105544
            [descr] => 
            [password] => 
            [delete_id] => D392
            [upload_id] => 6676FD881
        )

    [2] => Array
        (
            [id] => 45
            [name] => 12dsa32.jpg
            [size] => 49000
            [descr] => 
            [password] => 
            [delete_id] => 54C9
            [upload_id] => A58614C01
        )

)

Now I want is that if the link http://i41.tinypic.com/28cfub7.jpg is already upload I just add it to output array but maintain it in a order (if the link added was 2nd in array the output result should also show it in 2nd)

So what function should be used to remove the matched urls from input array and a function to add it output array in the order no.

// edited

Yes unset will do the thing but I want to maintain the order:

For example after unsetting the array looks like this:

 Array ( 
    [0] => http://i41.tinypic.com/3342r93.jpg 
    // [1] was removed
    [2] => http://i41.tinypic.com/12dsa32.jpg 
    )

but the output array would be

    Array
    (
        [0] => Array
            (
                [id] => 43
                [name] => 3342r93.jpg
                [size] => 362750
                [descr] => 
                [password] => 
                [delete_id] => 75CE
                [upload_id] => 75F45CAE1
            )

// this will become [1], so how can i add another output[1] and shift other 
// items after it to [2], [3] and so on...
        [1] => Array
            (
                [id] => 45
                [name] => 12dsa32.jpg
                [size] => 49000
                [descr] => 
                [password] => 
                [delete_id] => 54C9
                [upload_id] => A58614C01
            )

    )

Solution

  • Well, you can add it to the output array by doing something like:

    $OutArray[2] = $element;
    

    Where $element is another Array with the id, name, size (etc...) elements.

    As for removing from the array:

    unset($OutArray[2]);
    

    You may want to read Array (PHP manual).