Re: Echo a value from an arrays position

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

 



On 5/4/06, Brad Bonkoski <bbonkoski@xxxxxxxxxxxxxx> 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.

[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