开发者

How to get key values with foreach?

开发者 https://www.devze.com 2023-01-10 05:09 出处:网络
The Problem: Hi! I\'m making a file upload form where I can upload photos, I added multiple=\"\" to the input and for the name=\"upload_photo[]\" so I\'m able to upload multiple files. When I print

The Problem:


Hi! I'm making a file upload form where I can upload photos, I added multiple="" to the input and for the name="upload_photo[]" so I'm able to upload multiple files. When I print $_FILES['upload_photo'] I get this and I want to get the values of each key with foreach.

So for example I want to get just [name] what will be egg.jpg and green.jpg.

Array
(
    [name] => Array
        (
            [0] => egg.jpg
            [1] => green.jpg
        )

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

    [tmp_name] => Array
        (
            [0] => C:\wamp\tmp\php50E9.tmp
            [1] => C:\wamp\tmp\php50EA.tmp
        )

    [error] => Array
        (
            [0] => 0
            [1] => 0
        )

    [size] => Array
        (
            [0] => 24450
            [1] => 650开发者_JAVA技巧30
        )

)

This is what I have tried so far


But this gives me everything I don't have control over it.

foreach($_FILES['upload_photo'] as $keys => $file){

  foreach($_FILES['upload_photo'][$keys] as $key => $files){
   echo $files . "<br />";
  } 
 }


I think this is what you're trying to do, give this a shot:

foreach($_FILES['upload_photo'] as $photo) {
    echo $photo['name'];
}

I don't know how your $_FILES array got structured like this, and it appears you changed your question since i first answered, as my solution worked for the way your question was originally posted.

So for the new array structure of $_FILES you'll have to use nested loops, like so:

foreach($_FILES['upload_photo'] as $photo) {
    foreach($photo['name'] as $name) {
        echo $name;
    }
}


maybe this is the idea to help you when executing the following code with your given data in $_FILES it will print something like

upload 1

name = ...

size = ...


upload 2

name = ...

size = ...

$numUploads = count($_FILES['upload_photo']['name']);
for ($n = 0; $n < $numUploads; $n++) {

    echo 'upload ' . $n . '<br/>';
    foreach ($_FILES['upload_photo'] as $prop => $values) {

        echo $prop . ' = ' . $_FILES['upload_photo'][$prop][$n] . '<br/>';
    }

    echo '<hr/>';
}


If all you want is JUST the names of the uploaded files, then this will work:

foreach($_FILES['upload_photo']['name'] as $idx => $name) {
    echo "File #$idx: $name<br />"
}

If you're wanting to process each uploaded file, then you'd need something like this:

foreach(array_keys($_FILES['upload_photo']['name']) as $idx) {
   move_uploaded_file($_FILES['upload_photo']['tmp_name'][$idx], '/some/where/on/your/server');
}


Is this what you're after?

foreach($_FILES['upload_photo'] as $key => $value)
{
   for ($i = 0; $i < count($_FILES['upload_photo'][$key]); $i++)
   {
      echo $_FILES['upload_photo'][$key][$i];
   }

}

Not sure this is how I'd want to handle this situation, but I think it would work in your case.

edit: forgot the count

edit2: If you know that your array contains the keys shown in the sample above, to receive all the values from just the ['name'] array, you'd do the follow:

for ($i = 0; $i < count($_FILES['upload_photo']['name']); $i++)
{
   $name = $_FILES['upload_photo']['name'][$i];
   //do something else
}

OR

foreach ($_FILES['upload_photo']['name'] as $value)
{
   $name = $value;
   //do something else
}

As others have shown -

You seem to know the array keys involved / they are static, so the only thing left to do is figure out how many files there are - count the items inside the array and loop through it that many times.


Sounds like $_FILES is weird. So to get the names, what you want is simply...

foreach($_FILES['upload_photo']['name'] AS $name) {
    echo $name . "<br />";
}

Or, based on my comment below, you could do...

foreach($_FILES['upload_photo'] AS $key => $file) {
    if ($key == 'name') {
        foreach($file AS $name) {
          echo $name . "<br />";
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消