Jump to content

resetting the keys two sub-arrays within an array


rick.emmet

Recommended Posts

 

Hi Everyone,

I've been looking online for a way to reset the keys of sub arrays. Heres a portion of what I have:

Array
(
    [userfile] => Array
        (
            [name] => Array
                (
                    [5] => IMG_20170325_152954871_HDR.jpg
                )

            [type] => Array
                (
                    [5] => image/jpeg
                )

            [tmp_name] => Array
                (
                    [5] => /Applications/XAMPP/xamppfiles/temp/phpA49huQ
                )

        )

)

 

I've found a couple of examples that are close to what I'm looking for:

$_FILES = array_map('array_values', $_FILES);

	// And also tried

foreach ($_FILES as &$val) {
            $val = array_values($val); 
}

 

But both of them produce this output:

Array
(
    [userfile] => Array
        (
            [0] => Array
                (
                    [5] => IMG_20170325_152954871_HDR.jpg
                )

            [1] => Array
                (
                    [5] => image/jpeg
                )

            [2] => Array
                (
                    [5] => /Applications/XAMPP/xamppfiles/temp/phpA49huQ
                )
         )
    )

 

I want to go one level deeper and reset the sub array keys like so:

Array
(
    [userfile] => Array
        (
            [name] => Array
                (
                    [0] => IMG_20170325_152954871_HDR.jpg
                )

            [type] => Array
                (
                    [0] => image/jpeg
                )

            [tmp_name] => Array
                (
                    [0] => /Applications/XAMPP/xamppfiles/temp/phpA49huQ
                )
         )
    )

 

Everything I've tried has failed. Any ideas? Thanks so much.

Cheers,

Rick

Link to comment
Share on other sites

And to add to benanamen's question, why do you need the lowest values in yet another array? If there is just one value for name, type, etc why not have the array in this format

Array
(
    [userfile] => Array
    (
        [name] => IMG_20170325_152954871_HDR.jpg
 
        [type] => image/jpeg
 
        [tmp_name] => /Applications/XAMPP/xamppfiles/temp/phpA49huQ
     )
)
Link to comment
Share on other sites

Hello benanamen & psycho,
I want to reset the keys in the sub arrays & I don't want to reset the keys 'name', 'type', 'tmp_name', 'error' or 'size.' In this case,  the resulting $_FILES array can then be further processed, where the sub arrays are required ($_FILES['userfile']['name'][$num] for instance).

We didn't do much of anything with multi-dimensional arrays in the last PHP class I took. I've been studying, experimenting and testing multi-dimensional arrays for the last week or so, and have learned a bunch of neat tricks. This seems like it will be a useful tool for my bag of tricks.

 

Also, This is the way that the $_FILES array is set up, so, I want to be able to work with it as is - normally there will be many elements in this array.

If you could point me in the right direction, it will be very much appreciated.
Cheers,
Rick

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.