Re: Echo a value from an arrays position

From: Date: Fri, 05 May 2006 16:46:35 +0000
Subject: Re: Echo a value from an arrays position
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-235653@lists.php.net to get a copy of this message
On 5/4/06, Brad Bonkoski <bbonkoski@mediaguide.com> wrote:
Will this also work with an associative array? If this is what he is talking about, I tried it and it does not work.... I put this together though and it works, not sure if it is the *best* way though... <?php
        $colors =
array('white'=>'#ffffff','black'=>'#000000','blue'=>'#0000ff');
        $count = 0;
        foreach( $colors as $k => $v) {
                $count++;
                if( $count == 2 )
                        echo "\$colors[$k] => $v.\n";
        }
?> -Brad
One thing that might help you guys... now and in in the future are the functions var_dump() and print_r(). You can wrap these with <pre></pre> tags to make it all pretty and formatted for reading too. If you're unsure of what exactly is happening with your data try this: $colors = array('white'=>'#ffffff','black'=>'#000000','blue'=>'#0000ff'); print_r($colors); ... and you will see: Array ( [white] => #ffffff [black] => #000000 [blue] => #0000ff ) So you would call $colors['black'] to access #000000.

« previous php.general (#235653) next »