Assoc Arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David

    Assoc Arrays

    I have a script which retrives a list of files then reads the first
    two lines of each one of these files.
    Code:
    function retrieve_header s($username)
    {
    if(!file_exists ('c:/'.$username))
    return false;
    $directory = opendir("c:/$username//");
    $ary=array();
    while($file = readdir($direct ory))
    {
    if(!(is_dir($fi le) || ($file=='.') || ($file =='..')))
    {
    $fp = fopen("c:/$username/$file", "r");
    $recip = fgets($fp);
    $sender = fgets($fp);
    array_push($ary ,"$recip$sender ");
    fclose($fp);
    }
    }
    return $ary;
    }
    The idea is I want an array with $recip as the key and $sender as the
    value.
    Unfortunently the above code outputs an array like this:
    Code:
    0 => foo bar
    1 => foo bar
    2 => foo bar
    Any idea how I could go about this?
  • CountScubula

    #2
    Re: Assoc Arrays

    "David" <biffta@hotmail .com> wrote in message
    news:3eb239cb.0 401301013.72139 8a5@posting.goo gle.com...[color=blue]
    >...[/color]
    [color=blue]
    > $fp = fopen("c:/$username/$file", "r");
    > $recip = fgets($fp);
    > $sender = fgets($fp);
    > array_push($ary ,"$recip$sender ");
    > fclose($fp);
    > ....
    > The idea is I want an array with $recip as the key and $sender as the
    > value.[/color]


    change:
    array_push($ary ,"$recip$sender ");
    to:
    $ary[$recip] = $sender;
    --

    Mike Bradley
    http://www.gzentools.com -- free online php tools


    Comment

    • David

      #3
      Re: Assoc Arrays

      > change:[color=blue]
      > array_push($ary ,"$recip$sender ");
      > to:
      > $ary[$recip] = $sender;[/color]

      That works but it removes all duplicates which I do not want. Is there
      a way to do this without removing duplicate values?

      Comment

      • steven mestdagh

        #4
        Re: Assoc Arrays

        David <biffta@hotmail .com> wrote:[color=blue][color=green]
        > > change:
        > > array_push($ary ,"$recip$sender ");
        > > to:
        > > $ary[$recip] = $sender;[/color]
        >
        > That works but it removes all duplicates which I do not want. Is there
        > a way to do this without removing duplicate values?[/color]

        the key of an array element is unique. wouldn't it be easier to
        fill two arrays? or fill the array with two-element arrays, if you
        need everything inside one array.

        steven.

        Comment

        Working...