Re: Storing key values in an array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jochem -

The problem is the sort()! I did not realize that it replaces the key with an integer. Instead, I should have used ksort().

    $dir = opendir($doc_dir);
    while (false !== ($file = readdir($dir))) {
      if (($file != ".") && ($file != ".."))
        $file_list[$file] = $file;
    }
    // Debug code
    echo '<pre>';
    print_r($file_list);
    echo '</pre>';

    sort($file_list);  <-----------<<<<

    foreach ($file_list as $key => $val) {
      echo $key . " => " . $val . "<br>";
    }

Here are the result:

Array
(
    [mailings] => mailings
    [cases] => cases
)

0 => cases
1 => mailings
  	
Todd

Jochem Maas wrote:
Todd Cary wrote:

I am using an array to populate a drop-down and I would like to have the same value in the key as the value. Using the following, the key is 0,1,2,3...etc. How can I correct that?


what is there to correct - you are telling me that the value of $file show up as a string and then magically it becomes an integer (but just while you are designating the key)

listen you've just shown us a simple loop to stuff some file/dir names into an array where by the file/dir name is the assoc. key as well as the value of any given item in said array - WHERE THE F*** IS THE DROPDOWN RELATED CODE?


$file_list = array(); $dir = opendir($doc_dir); while (false !== ($file = readdir($dir))) { if (($file != ".") && ($file != "..")) $file_list[$file] = $file; <---------------<<<<< }


what happens if you place the following right after the code you mention (just to prove that what you say about the key is wrong):

echo '<pre>';
print_r($file_list);
echo '</pre>';


Todd


-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux