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

3
  • So why do you want to rename keys? Commented Apr 23, 2017 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 Commented Apr 23, 2017 at 19:08
  • 1
    Possible duplicate of Reset keys of array elements in php? Commented Apr 23, 2017 at 19:13

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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";
    } 
  }

?>

Comments

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',
      ),
    ),
  ),
)

2 Comments

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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.