SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: array_keys

  1. #1
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    array_keys

    What's wrong with this syntax?

    PHP Code:
    echo array_keys($_SESSION['tablearray'])[0]; 
    Ballot-Box.net - free polls for webmasters
    FormLog.com - free form processor

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Trying to access array values directly from a function call, as you are trying to do, is currently* not allowed. You either need to use some other way to get at the information that you want.

    PHP Code:
    // preferred
    $keys array_keys($_SESSION['tablearray']);
    echo 
    $keys[0];
    // alternative (both of the following assume you want to get the first key)
    echo current(array_keys($_SESSION['tablearray']));
    // or
    reset($_SESSION['tablearray']);
    echo 
    key($_SESSION['tablearray']); 
    * For what it's worth, PHP 5.4.0 will introduce the syntax that you are trying to use under the name "function array dereferencing".
    Salathe
    Software Developer and PHP Manual Author.


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
  •