create an array value for each key without a new array

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
hoteljamaca
New php-forum User
New php-forum User
Posts: 12
Joined: Sat Feb 28, 2009 11:13 am

Fri Apr 06, 2012 9:24 am

Hi to @all !

I have a simple array like this :

Code: Select all

$a = array('foo','bar','banana','choco');
I can't just do a :

Code: Select all

$a['foo'] = 'something';
because i don't know what things i have on my array (it's a files list).

The following code works but it have to create a second array called $b (not good for memory usage) :

Code: Select all

$b = array();
foreach ($a as $v) {
$b[$v] = 'something';
}
Is it possible to directly populate $a without creating a $b ?

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2192
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Fri Apr 06, 2012 10:26 am

Jamaca,
I need a little more info from you..

There are many ways to do this, give me more of a real world example of what you're looking for.

hoteljamaca
New php-forum User
New php-forum User
Posts: 12
Joined: Sat Feb 28, 2009 11:13 am

Fri Apr 06, 2012 11:39 am

i would like to scan a directory and all subdirectories for files (not folders) using the following function :
This function works @ 100%

Code: Select all

protected function listFiles($dir) {
        $dh=opendir($dir);
        if($dh!=false)
        {
            $files=Array();
            $inner_files=Array();
            while($file=readdir($dh))
            {
                if($file!='.'&&$file!='..'&&$file[0]!='.')
                {
                    if(is_dir($dir.'/'.$file))
                    {
                        $inner_files=$this->listFiles($dir.'/'.$file);
                        if(is_array($inner_files)) 
                            $files=array_merge($files,$inner_files);
                    }
                    else
                    {
                        array_push($files,$dir.'/'.$file);
                    }
                }
            }
            closedir($dh);
            return $files;
        }
    }
Then I would like to associate $files to an unique ID (a new random generated name),rename all and insert all things to database.
The "rename all"+"move all" process needs an associative array.

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2192
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Fri Apr 06, 2012 12:29 pm

easy enough.

$counted = 1;

if (!isset($array[$file])) {
$array[$file] = $file;
} else {
$name = $file$counted;
$array[$name] = $file;
$counted++;
}

hoteljamaca
New php-forum User
New php-forum User
Posts: 12
Joined: Sat Feb 28, 2009 11:13 am

Sat Apr 07, 2012 2:28 am

$file$counted;
There is a syntax error here.

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2192
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Mon Apr 09, 2012 4:35 am

$name = $file.''.$counted;

Sorry..

hoteljamaca
New php-forum User
New php-forum User
Posts: 12
Joined: Sat Feb 28, 2009 11:13 am

Tue Apr 17, 2012 12:20 am

ok thanks

Post Reply