Multi Dimensional array keys

Discussion in 'PHP' started by Silver89, Jan 18, 2010.

  1. #1
    This is driving me crazy... can't seem to get the key of the arrays that are set initially?

    
    <?
    $fileArray = array("Directory" => array(), "File" => array());
    
    if ($handle = opendir('.'))
    {
        while (false !== ($file = readdir($handle)))
    	{
            if ($file != "." && $file != "..")
    		{
    			if(is_dir($file))
    			{
    			$fileArray["Directory"][] = $file;
    			}
    			else
    			{
    			$fileArray["File"][] = $file;
    			}
    		}
        }
        closedir($handle);
    }
    
    rsort($fileArray);
    
    
    foreach ($fileArray as $fileType)
    {
    ?>
    	<h2><?=print(array_keys($fileArray))?></h2>
    <?
    	foreach($fileType as $file)
    	{
    ?>		
    	<a href="/<?=$file?>"><?=$file?></a><br />
    <?
    	}
    }
    ?>
    
    PHP:
     
    Silver89, Jan 18, 2010 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    foreach ($fileArray as $key =>$fileType)
    PHP:
    Try that in the foreach and then use $key
     
    JAY6390, Jan 18, 2010 IP
  3. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #3
    That gives 0 and 1 so I guess that's kind of right, just need the text value of they key which I can't seem to get.
     
    Silver89, Jan 18, 2010 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Can you add this line both before and after the rsort function
    echo '<pre>'.print_r($fileArray, true).'</pre>';
    PHP:
    And show the output here for the two arrays
     
    JAY6390, Jan 18, 2010 IP
  5. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #5
    
    Array
    (
        [0] => Array
            (
                [0] => index.php
                [1] => sqlite_test.php
                [2] => apache_pb.png
                [3] => phpinfo.php
                [4] => index.htm.old
            )
    
        [1] => Array
            (
                [0] => Test
            )
    
    )
    
    Code (markup):
     
    Silver89, Jan 18, 2010 IP
  6. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #6
    This is after the rsort I take it. Change rsort to arsort and see if that fixes it. rsort doesn't maintain the keys by default (see the manual - http://www.php.net/rsort)
     
    JAY6390, Jan 18, 2010 IP
    ramakrishna p likes this.
  7. Silver89

    Silver89 Notable Member

    Messages:
    2,243
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    205
    #7
    Excellent that sorted it out, don't really use arrays too much so I just used the first sort I found.
     
    Silver89, Jan 18, 2010 IP