Re: Echo a value from an arrays position

From: Date: Thu, 04 May 2006 12:35:50 +0000
Subject: Re: Echo a value from an arrays position
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-235486@lists.php.net to get a copy of this message
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 Jay Blanchard wrote:
[snip] Is there any way to call for an element value in an array by the position? Like position 2 in the array and not the key name. [/snip] I hate to say this, but you really need to RTFM http://www.php.net/array To get a value from position 2 in an array you use $arrayName[1] (all array elements start numbering at 0, unless you do something to the array to change it).


« previous php.general (#235486) next »