resetting the keys two sub-arrays within an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rick.emmet
Forum Commoner
Posts: 70
Joined: Fri Aug 14, 2009 9:43 am

resetting the keys two sub-arrays within an array

Post by rick.emmet »

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

Code: Select all

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:

Code: Select all

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

	// And also tried...

foreach ($_FILES as &$val) {
            $val = array_values($val); 
But both of them produce this output:

Code: Select all

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:

Code: Select all

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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: resetting the keys two sub-arrays within an array

Post by Christopher »

Maybe something like:

Code: Select all

$myfiles = [];
foreach ($_FILES as $file => $filedata) {
    foreach ($filedata as $key => $value) {
        $myfiles[$file][$key][0] = $filedata[$key][5];
    }
}
(#10850)
Post Reply