May 5, 2012

Get all array keys that is not equal to X

Question by Oyvind Andersson

I have several files uploaded with a form that is processed by a PHP script. There are several “categories” that I need to have handles separate ways. In other words, I need some of the files in the $_FILES array to be stored someplace, and some others stored in other places.

So what I need is all files (array keys) that are not $_FILES[‘filename_form’] to be stored / used separably.

The reason why I need this is because the amount of files being uploaded are dynamic, but always either $_FILES[‘filename_type1’] or $_FILES[‘filename_type2_*] where * is a number that increments when more files are input from the form (dynamically)

Might be a very simple solution, either I’m too tired or maybe I’ve been staring too much at the code, but all I know is I need a solution before tomorrow morning, so it’s time sensitive =P

Appreciate all the help I can get.
Thank you!

Answer by Zombaya

$files = array();
$filenames = array();
foreach($_FILES as $name => $fileInfo)
{
    if($name != 'filename_form')
    {
        $files[$name] = $fileInfo;
        $filenames[] = $name;
    }
}

Answer by Starx

Here is an alternate way

$keys = array_keys($thearray);
$filteredArray = array_filter($keys, 'fiterKeys');

function filterKeys($item) {
    if($item != 'valuetocheck') return true;
    else return false;
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!