SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    563
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How to get first value from array ?

    Hello,

    I have a dynamically generated array which has data like following:

    PHP Code:
    Array
    (
        [
    4] => 3
        
    [3] => 1

    How to get the first value from the array as I don't know whats going to be the values. So as in this the first is 4 and it can be anything.

    How do I get the first value ? I want to get both key and its value.

    echo array{0} or something like that. Please help.

    Thanks.

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $array 
    = array(
      
    => 1,
      
    => 6
    );

    /*
      Just to be safe
    */
    reset($array);

    /*
      Get first value
    */
    echo current($array), PHP_EOL ;

    /*
      Get key for first value
    */
    echo key($array), PHP_EOL ;
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    here one (other) way:
    PHP Code:
    $x = array ( => ,  => );
    $y array_chunk($x11);
    var_dump$y[0]); 

  4. #4
    SitePoint Evangelist Tapan's Avatar
    Join Date
    May 2005
    Location
    India
    Posts
    563
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks for the reply. I just found this function in php manual:

    $keys = array_keys($array);

    I got the first value then using $keys[0].

    Then echo array[$keys[0]];

    Works!

    Thanks.

  5. #5
    SitePoint Zealot zbing's Avatar
    Join Date
    Jun 2002
    Location
    Lisbon, Portugal
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can get a list of all the array keys by using the array_keys() function.

    PHP Code:
    $myArray = array (=> 3=> 1);
    $arrayKeys array_keys($myArray);

    // the first element of your array is:
    echo $myArray[$arrayKeys[0]]; 
    zbing

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    For what it's worth, reset() will return the first value (negating the need for calling current()).
    Salathe
    Software Developer and PHP Manual Author.

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    That seems a little strange, I'd expect (wrongly, apparently) a true/false from that.

    ho-hum.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #8
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    That seems a little strange, I'd expect (wrongly, apparently) a true/false from that.
    It behaves similarly to the other associated functions, here's a quick overview of what they return.

    reset - First value in array
    end - Last value in array
    each - Array containing key/value pair (twice) for current item
    current - Current value
    key - Current key
    next - Next value in array
    prev - Previous value in array

    They will return FALSE if the function cannot get what it wants (e.g. reset() with an empty array, prev() when at the beginning of an array, etc.).
    Salathe
    Software Developer and PHP Manual Author.

  9. #9
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    If you only want the value, you can also use:

    PHP Code:
    $array = array('one''two''three');
    list(
    $first$second$third) = $array;
    echo 
    $first


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •