How to re order the index in a array after removing a record

Hello,

How do I re-order the index after removing a record from a array?
I used unset() to remove a record from the array.

I attached a image of my results. Looking at the image you can see for example that I removed index 1 leaving index 0 and index 2.

what i like to do is reorder the index for name, type, tmp_name, error, and size so that its back to index 0 and index 1.

My attempts so far hasn’t been successful, how do i go about re ordering the index?

any help you can provide would be greatly appreciated.

I have not tried it but it appears as though it may work… have you tried creating a blank array then use array_merge(…)

http://php.net/manual/en/function.array-merge.php

If that does not work then there are quite a few other array functions you could try:

http://php.net/manual/en/ref.array.php

You don’t need it.
In PHP you don’t need consequent keys to iterate over array.

Just use foreach with key fetching syntax, and use that key to address other arrays.

By the way, speaking of the $_FILES array, I’d suggest you to name your multiple fields not as mypics[]...mypics[] but as mypics1...mypics2...mypicsX. It will make $_FILES array WAY more convenient, letting you to just foreach over it, as well as removing elements without hassle.

1 Like
unset($array[1]);
$array = array_values($array);

That will rebuild keys

2 Likes

Hi john_Betong,

Thanks for the replay back. I have seen the array_merge() in my searches but bypass it thinking there has to be an easier way to achieve what I’m looking for. having said that I like to maintain the global availability of $_FILES. the class im working, all it does is validate the images. I’m using a different class to move the images.

1 Like

Hi Colshrapnel,

thanks for the reply back.
I haven’t thought of the key fetching but will google that and get little more understanding.

now, regards your last comments, what do you mean by myypics2…mypicsX.

what i have.
i have a form and the upload function is controlled by javascript so the user can add more images by clicking add more images button.
and the input field as such :



please expand on your comments im interested in what you mean by naming it differently.

Hi megazoid,

Thanks for the reply back,

i did come across the array_values() but not sure what i was doing wrong but it wouldn’t work for me for some reason.
I’m not even sure how to use it in multidimensional array or with $_FILES.

any chance you have examples you could share?

Just like I said, instead of making it

name="mypics[]">
name="mypics[]">
name="mypics[]">

make it

name="mypics1">
name="mypics2">
name="mypics3">

And your $_FILES array will be a lot more handy.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.