-1

I am having an issue getting the inner array key/value pairs correctly. I have the outer array correct but the inner arrays just have index numbers as keys instead of me setting the key to what I am wanting. Seems like I'm missing a step in the formation of the inner array but I'm not sure what it is.

My current code now:

$path = './downloads/Current/v5.5/';
$blacklist = array('orig55205Web', 'SQL Files', '.', '..');

foreach (new DirectoryIterator($path) as $folder) {
    if($folder->isDot() || in_array($folder, $blacklist)) continue;

    if($folder->isDir()) {
        $item = $folder->getFilename();
        $versions[$item] = array();
        
        if ($handle = opendir($path . $item)) {
            while (false !== ($file = readdir($handle))) {
                if (!in_array($file, $blacklist)) {
                    array_push($versions[$item], $file);
                }
                asort($versions[$item]);
                $versions[$item] = array_values($versions[$item]);
            }
        }
        closedir($handle);
    }
}
ksort($versions);
print_r($versions);

My output looks like this currently:

Array
(
    [55106Web] => Array
        (
            [0] => 55106.txt
            [1] => ClientSetup.exe
            [2] => ClientSetup32.exe
            [3] => Setup.exe
            [4] => Setup32.exe
        )

    [55122Web] => Array
        (
            [0] => 55122.txt
            [1] => ClientSetup.exe
            [2] => ClientSetup32.exe
            [3] => Setup.exe
            [4] => Setup32.exe
        )
 )

What i WANT it to output:

Array
(
    [55106Web] => Array
        (
            [Version] => 55106.txt
            [CS64] => ClientSetup.exe
            [CS32] => ClientSetup32.exe
            [S64] => Setup.exe
            [S32] => Setup32.exe
        )

    [55122Web] => Array
        (
            [Version] => 55122.txt
            [CS64] => ClientSetup.exe
            [CS32] => ClientSetup32.exe
            [S64] => Setup.exe
            [S32] => Setup32.exe
        )
 )
4
  • Can you show what you want the output to be? Commented Feb 17, 2014 at 14:50
  • Sorry about that Patrick Q, forgot the output piece Commented Feb 17, 2014 at 14:53
  • Remove array_values() call Commented Feb 17, 2014 at 14:58
  • That just changes the sorting of the keys, instead i want to name the keys something specific Commented Feb 17, 2014 at 15:03

1 Answer 1

1

There are two issues here. First, you're not doing anything that assigns string-based indexes to the inner array.

Second, even if you were, those indexes would be removed as a result of your use of array_values From the docs array_values() returns all the values from the array and indexes the array numerically.

So you should assign the indexes (see below), and remove the call to array_values.

This may not exactly suit your needs 100%, but should get you going in the right direction.

$indexesArray = array("Version", "CS64", "CS32", "S64", "S32");
$i = 0;
while (false !== ($file = readdir($handle))) {
    if (!in_array($file, $blacklist)) {
        $versions[$item][$indexesArray[$i]] = $file;
        $i++
    }
}
asort($versions[$item]);
Sign up to request clarification or add additional context in comments.

9 Comments

That doesnt solve my issue with being able to name the keys. Instead of numerical keys i want the name the key something specific
@user1784477 "i want the name the key something specific" So add that to your code. Right now, there's nothing in there that attempts to specify the index. i.e. Where are you getting Version, CS64, CS32, etc? Where are you trying to assign those as indexes?
right now the 'version, etc..' values will be hardcoded but im assuming that i need to have the index name set during this step? array_push($versions[$item], $file);
I havent written anything to handle the naming yet, i have been stuck on getting the key name set in the first place..
Causes errors with array_push because requiring parameters array_push($versions[$item][$i], $file); instead of array_push($versions[$item][$i] = $file); Where does $indexesArray come into play?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.