Forum Moderators: coopster

Message Too Old, No Replies

Reordering advanced arrays

After picture submission ($_FILES)

         

tomda

1:27 pm on Dec 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

I have a form where user can send up to three pictures.
<input type="file" name="imagefile[]">
<input type="file" name="imagefile[]">
<input type="file" name="imagefile[]">, etc...

Now, this what's happening when someone post two pictures but escape the second input (that is use the 1st and 3rd input).

I get this (you can see that [1] is empty) :

Array (
[name]=>Array([0]=>photo.jpg [1]=> [2]=>photo2.jpg)
[type]=>Array([0]=>image/jpeg [1]=> [2]=>image/jpeg)
[tmp_name]=>Array([0]=>D:\3F6.tmp [1]=> [2]=>D:\3F9.tmp)
[error]=>Array([0]=>0 [1]=>4 [2]=>0)
[size]=>Array([0]=>24426 [1]=>0 [2]=>28712)
)

Is there a way to rearrange the array so that empty key are at the end of the array (I need this: [name]=>Array([0]=>photo.jpg [1]=>photo2.jpg [2]=> )

Thanks

whoisgregg

3:59 pm on Dec 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Probably be easier in your loop to just check that the array is populated THEN do your processing. This is good practice anyways for when people or bots come around and submit your forms with empty values.

pseudocode (off the top of my head):

for($i=0; $i<count($_FILES); $i++)){
  if ( isset($_FILES['name'][$i]) && ($_FILES['name'][$i]!='') ) {
    // do your processing
  }
}

tomda

5:20 am on Dec 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks WhoisGreg,

Indeed, this is what I did yesterday (use foreach and remove empty value) and I got this:


Array (
[name]=>Array([0]=>photo.jpg [2]=>photo2.jpg)
[type]=>Array([0]=>image/jpeg [2]=>image/jpeg)
[tmp_name]=>Array([0]=>D:\3F6.tmp [2]=>D:\3F9.tmp)
[error]=>Array([0]=>0 [2]=>0)
[size]=>Array([0]=>24426 [2]=>28712)
)

As you can see the key[1] has been removed.
I did some search yesterday on how I could reinitialise keys so that instead of having [0] & [2], I have [0] & [1].

dreamcatcher

10:44 am on Dec 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to go down the multi dimensional route, you can assign your data to a new array. Like this:

<input type="file" name="imagefile[]">


$new_array = array();

for ($i=0; $i<count($_FILES['imagefile']); $i++)
{

if (!empty($_FILES['imagefile']['tmp_name'][$i]))
{
$new_array[0][$i] = $_FILES['imagefile']['tmp_name'][$i];
$new_array[1][$i] = $_FILES['imagefile']['name'][$i];
$new_array[2][$i] = $_FILES['imagefile']['type'][$i];
$new_array[3][$i] = $_FILES['imagefile']['size'][$i];
}

}

Then work with the new array, $new_array.

for ($i=0; $i<count($new_array); $i++)
{
echo "Temp Name :" . $new_array[0][$i] . "<br>";
echo "Name :" . $new_array[1][$i] . "<br>";
echo "Type :" . $new_array[2][$i] . "<br>";
echo "Size :" . $new_array[3][$i] . "<br><br>";
}

Good luck.

dc

whoisgregg

6:01 pm on Dec 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd modify dreamcatcher's code like so:

$new_array[0][] = $_FILES['imagefile']['tmp_name'][$i];
$new_array[1][] = $_FILES['imagefile']['name'][$i];
$new_array[2][] = $_FILES['imagefile']['type'][$i];
$new_array[3][] = $_FILES['imagefile']['size'][$i];

Otherwise, you're just building a new array that will have the same keys. (Since you are using the increment as the new key.)

My point was not to remove the empty key, but to just ignore the empty one. Why do you need the keys to be accurately numbered? If the problem is that a

count($_FILE['name'])
isn't giving an accurate count, I'd just increment a counter inside my check for an empty array.

coopster

6:14 pm on Dec 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




...on how I could reinitialise keys so that instead of having [0] & [2], I have [0] & [1].

array_values() is usually the ticket to get that done. However, this is a multidimensional array as you have noted so I think your best option will be a user defined function with a foreach loop as you have already done. A combination of the two will probably get you where you want to end up then. Something along these lines ...

function filter_FILES() 
{
$blanksfound = false;
foreach ($_FILES['name'] as $k => $v) {
if (!$v) {
$blanksfound = true;
foreach ($_FILES as $fkey => $fval) {
unset($_FILES[$fkey][$k]);
}
}
}
if ($blanksfound) {
foreach ($_FILES as $fkey => $fval) {
$_FILES[$fkey] = array_values($_FILES[$fkey]);
}
}
}

You could also use an array_multisort() if you wanted that array in a different order. Use that in place of the array_values(). array_multisort() will re-index numeric keys too.

coopster

6:17 pm on Dec 14, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Added:
I agree with whoisgregg on this one though, why even bother getting rid of anything anyway? Loop and process based on the conditions you specify, ignoring that which doesn't meet certain criteria.

dreamcatcher

2:46 am on Dec 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Otherwise, you're just building a new array that will have the same keys. (Since you are using the increment as the new key.)

Thanks whoisgregg, my mistake. :)

tomda

5:16 am on Dec 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey PHP gurus :)

Yeah, in fact, Whoisgregg and Coopster are right, what I was I trying to do is stupid because there is no use of getting rid off empty values... As you said, I can just process what is not empty!

Somehow, when you spend too much time on one thing, you tend to forget the basic and make things complicated :(

Again, thanks for your help and tips, much appreciated.

Tomda

dreamcatcher

9:15 am on Dec 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, I agree with them too tomda. I just thought you had a purpose for it? Guess not.

dc