SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    725
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Question PHP: Get the data of a first array

    Hey everyone,

    I have a question regarding array in PHP. How do you get the data array of the first element of an array when the keys are unknown.

    Code:
    array (
    	
    	[???] => array(
    			'id' => 50,
    			'name' => 'John',
    			'email' => 'jogn@example.com'
    	
    	),
    	[???] => array(
    			'id' => 56,
    			'name' => 'Pony',
    			'email' => 'pony@example.com'
    	
    	)
    
    
    );

    Thanks

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The array_keys() function is the best function i can see that you could use.
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  3. #3
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    Arrays have an internal pointer, much akin to how a file pointer works. Just reset the array and read the current one. In fact, the reset command when done on an array returns the first record.

    $firstRecord=reset($yourArray);

    Relevant commands you should read up on:
    PHP: reset - Manual
    PHP: next - Manual
    PHP: prev - Manual
    PHP: end - Manual
    PHP: each - Manual
    PHP: current - Manual

    The 'foreach' construct actually performs a reset() when it starts, then does a bunch of each().

  4. #4
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    725
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    @deathshadow60,

    If I use the reset() function, will it not destroy my current keys?

  5. #5
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    reset has nothing to do with keys -- the internal pointer is used for iterating the array, not for populating it. Its no different than performing a foreach on it.

    Code:
    reset($myarray);
    while ( list($key,$data)=each($myarray) ) {
      /* do something here */
    }
    is functionally identical to:

    Code:
    foreach ($myarray as $key => $data) {
      /* do something here */
    }
    Though nowhere near as elegant. NONE of those commands change the contents of the array or it's length or keys or anything else. The only thing that's changed is the internal read pointer... READ pointer.

    So for example if you want the first record of the array and it's key...

    $firstRecord=reset($myarray);
    $firstKey=key($myarray);

    Doesn't change the array's values at all.

    *NOTE* do not try to use those functions inside foreach though -- foreach uses that same internal read pointer.

  6. #6
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    725
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thank u so much deathshadow60 for the deatailed explanation.


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
  •