1

My code has made a pass through the $_FILES array and unset a number of files. The array now has keys that don't start with zero and are also out of sequence.

[userfile] => Array
    (
        [name] => Array
            (
                [2] => IMG_20170325_124043610_HDR.jpg
                [3] => video_icon.png
                [5] => watersports.gif
                [7] => IMG_20170325_153726906_HDR.jpg
            )

I would like to rename the keys sequentially starting from zero, like this:

[userfile] => Array
    (
        [name] => Array
            (
                [0] => IMG_20170325_124043610_HDR.jpg
                [1] => video_icon.png
                [2] => watersports.gif
                [3] => IMG_20170325_153726906_HDR.jpg
            )

I don't want to change the key values for [ userfile] [name] or any of the other non-numeric keys. Is there a function for this? I would like to do something along these lines:

// FILE COUNT IS PROVIDED BY CODE ABOVE

// Is $num equal to $fileCount? 
$num = 0;

// Change the value of the key through iteration
while ($num < $fileCount) {
   // need a built in function that allows the key change
   **reset_key**($_FILES['userfile']['name'][$num]);
   **reset_key**($_FILES['userfile']['type'][$num]);
   **reset_key**($_FILES['userfile']['tmp_name'][$num]);
   **reset_key**($_FILES['userfile']['error'][$num]);
   **reset_key**($_FILES['userfile']['size'][$num]);
}
$num++;

Is this even the correct approach, or should I be trying something else here? Thanks so much for your input!

Cheers,

shackleton

shackleton
  • 49
  • 2
  • So why do you want to rename keys? – u_mulder Apr 23 '17 at 19:06
  • I don't really know why, but if you want to reset keys, you could do something like `$arr = array_values($arr);`. Example: [https://3v4l.org/K5evY](https://3v4l.org/K5evY) – FirstOne Apr 23 '17 at 19:08
  • 1
    Possible duplicate of [Reset keys of array elements in php?](http://stackoverflow.com/questions/10492839/reset-keys-of-array-elements-in-php) – Pedro Lobito Apr 23 '17 at 19:13

3 Answers3

0

There's no need to reset your keys. You can iterate over your $_FILES data with a simple key => value foreach and grab values with the same key from other subarrays:

foreach ($_FILES['userfile']['name'] as $key => $value) {
    echo 'Name: ', $value;
    echo 'Tmp_name: ',$_FILES['userfile']['tmp_name'][$key];
    echo 'Size: ', $_FILES['userfile']['size'][$key];
}

If you still want to reset keys of subarrays, then use array_values, but beware - you need to reset values in all sub-arrays of $_FILES['userfile']: name, tmp_name, error, size, type.

u_mulder
  • 54,101
  • 5
  • 48
  • 64
0

use array_values()

Code:

<?php

$file= array(

            $arr1 = array
            (
                23 => 'IMG_20170325_124043610_HDR.jpg',
                4 => 'video_icon.png',
                55 => 'watersports.gif',
                7 => 'IMG_20170325_153726906_HDR.jpg'
            ),

      $arr2 = array
            (
                23 => 'IMG_20170325_124043610_HDR.jpg',
                43 => 'video_icon.png',
                54 => 'watersports.gif',
                7 => 'IMG_20170325_153726906_HDR.jpg'
            )

            );


for ($i=0; $i<count($file); $i++){

    $arr=$file[$i];
    $arr= array_values($arr);
    sort($arr);

    foreach ($arr as $key => $val) {
       echo "$key = $val\n";
    } 
  }

?>
Leo
  • 7,274
  • 5
  • 26
  • 48
0

array_walk() with array_values() one-liner to the rescue:

array_walk($FILES,function(&$v){$v["userfile"]["name"]=array_values($v["userfile"]["name"]);});

array_walk() breakdown:

array_walk(
    // changed $_FILES to $FILES for testing only
    $FILES, // this is the input array to iterate
    function(&$v){  // $v is each 1st level subarray in $FILES
        //   ^--  & means alter each original subarray from $FILES
        $v["userfile"]["name"]=  // only redeclare/overwrite the subarray's subarray called "name"
            array_values($v["userfile"]["name"]);  // create a new array with zero-indexed keys and the same values as the original subarray's subarray
    }
);

If you use this array as input:

$FILES=array(
    array(
        "userfile" => array(
            "name" => array(
                2 => "IMG_20170325_124043610_HDR.jpg",
                3 => "video_icon.png",
                5 => "watersports.gif",
                7 => "IMG_20170325_153726906_HDR.jpg"
            )
        )
    ),
    array(
        "userfile" => array(
            "name" => array(
                9 => "IMG_20170325_124043610_HDR.jpg",
                13 => "video_icon.png",
                55 => "watersports.gif",
                97 => "IMG_20170325_153726906_HDR.jpg"
            )
        )
    ),
    array(
        "userfile" => array(
            "name" => array(
                1 => "IMG_20170325_124043610_HDR.jpg",
                88 => "video_icon.png",
                7 => "watersports.gif",
                10 => "IMG_20170325_153726906_HDR.jpg"
            )
        )
    )
);

After array_walk(), var_export($FILES); will output:

array (
  0 => 
  array (
    'userfile' => 
    array (
      'name' => 
      array (
        0 => 'IMG_20170325_124043610_HDR.jpg',
        1 => 'video_icon.png',
        2 => 'watersports.gif',
        3 => 'IMG_20170325_153726906_HDR.jpg',
      ),
    ),
  ),
  1 => 
  array (
    'userfile' => 
    array (
      'name' => 
      array (
        0 => 'IMG_20170325_124043610_HDR.jpg',
        1 => 'video_icon.png',
        2 => 'watersports.gif',
        3 => 'IMG_20170325_153726906_HDR.jpg',
      ),
    ),
  ),
  2 => 
  array (
    'userfile' => 
    array (
      'name' => 
      array (
        0 => 'IMG_20170325_124043610_HDR.jpg',
        1 => 'video_icon.png',
        2 => 'watersports.gif',
        3 => 'IMG_20170325_153726906_HDR.jpg',
      ),
    ),
  ),
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • Hello Mickmackusa, Thank you very much for the input. I knew that there were other ways to handle this problem and I was trying to figure out how the PHP engine would run using different approaches. I spent several days experimenting, doing everything I could think of, and testing to see the results. I know that I'll be using a lot of arrays and array functions - and we hardly did any of this in class. It amazes me to see how this works. I'll be able to use this approach on many projects. I never would have been able to intuit that one-liner-to-the-rescue. Thanks again! Cheers, Rick – shackleton Apr 26 '17 at 00:12
  • @shackleton I added a breakdown of `array_walk()` to help explain how it works -- hopefully that makes my answer more clear for you and other SO readers. – mickmackusa Apr 26 '17 at 00:50