Thread: Building array

    #1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,671
    Rep Power
    171

    Building array


    Hi;

    I want to end up with
    Code:
    Array
    (
        [1378415589] => 5228f3e5ad28b.png
            
        [1378415595] => 5228f3eb9d2f4.png
          
        [1378415605] => 5228f3f5e3567.jpg
          
    )
    But instead I end up with
    Code:
    Array
    (
        [0] => Array
            (
                [1378415589] => 5228f3e5ad28b.png
            )
    
        [1] => Array
            (
                [1378415595] => 5228f3eb9d2f4.png
            )
    
        [2] => Array
            (
                [1378415605] => 5228f3f5e3567.jpg
            )
    
     )
    How can I change my code?
    PHP Code:
    while (false !== ($entry readdir($handle)))
        {
            if(
    $entry!='..' && $entry!='.' && $entry!='.DS_Store' && file_exists("photos/".$entry))
                {
                    
    $array_files[] = array(filemtime('photos/'.$entry)=>$entry );
                }
        } 
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Um, by not appending arrays to $array_files?

    Your program does exactly what you told it to:

    PHP Code:
    $array_files[] = array(filemtime('photos/'.$entry)=>$entry ); 
    On the right, you have an associative array with one entry. The key is filemtime(), the value is $entry. And then you push this array to $array_files.

    But it seems what you actually wanna do is make $array_files a big associative array:

    PHP Code:
    $array_files[filemtime('photos/'.$entry)] = $entry
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,671
    Rep Power
    171
    Originally Posted by Jacques1[PHP
    $array_files[filemtime('photos/'.$entry)] = $entry;[/PHP]
    That was just so simple

IMN logo majestic logo threadwatch logo seochat tools logo