Re: Storing key values in an array

From: Date: Fri, 21 Jan 2005 22:11:11 +0000
Subject: Re: Storing key values in an array
References: 1 2  Groups: php.general php.general 
Request: Send a blank email to php-general+get-207004@lists.php.net to get a copy of this message
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


« previous php.general (#207004) next »