SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,516
    Mentioned
    51 Post(s)
    Tagged
    1 Thread(s)

    getting an array item's value when the name is unknown

    I'm trying to retrieve the array value of the first named array item, like below, but without knowing ahead of time what the name is.

    Code:
    $myarray;
    $myarray['something']="the value";
    $myarray['another something']="another value";
    
    <input type="text" value="<?= key($myarray) ?>" />
    Using Key() gets me the name. Would this be the correct way to go about it, or is it "too wordy":

    $myarray[key($myarray)]


    Thanks

  2. #2
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seems reasonable to me. If you want to get all of them, you can use foreach like so:

    PHP Code:
    foreach( $array as $key => $value
    {

    But if you only need the first one that seems reasonable.

    Edit: I did not realize you were wanting the first value, and not the first key. If you only want the first value look into http://us3.php.net/manual/en/function.current.php (current)

  3. #3
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This works:

    PHP Code:
    <?php

    $input 
    = array("color1" => "red","color2" => "blue""yellow""number1" => "14235""number2" =>  "4532""number3" =>  "21359""string1" =>  "This can be a million values...""string2" => "But it will only""string3" => "return the first value");


    $values array_values($input);

    echo 
    $values['0']; // displays red
    ?>
    How does that make your feel?

  4. #4
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,516
    Mentioned
    51 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by UFTimmy View Post
    If you only want the first value look into http://us3.php.net/manual/en/function.current.php (current)
    Yep, that did the trick

    Thanks

  5. #5
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,516
    Mentioned
    51 Post(s)
    Tagged
    1 Thread(s)
    ok, I take it back...content($myarray) didn't do it. I went back to my verbose method of $myarray[key($myarray)], and it work fine.

    hmm....

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    if you want the first item then...
    PHP Code:
    var_dumparray_shift( array( 'color' => 'red''animal' => 'cat' ) ) ); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,516
    Mentioned
    51 Post(s)
    Tagged
    1 Thread(s)
    That won't help if I don't know the array keys & values going in.

    I'm trying to get the value of the first array item without knowing the key name of that first array item. They aren't numerical, which was the problem.


    $myarray;
    $thevalue = $myarray[key($myarray)];

    Like I said, this works, but to me, seems sloppy.

  8. #8
    Sesame Street Iimitk's Avatar
    Join Date
    Feb 2006
    Posts
    662
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As MadMike has already explained, this is what array_values() is perfectly suitable for.

    PHP Code:
    $myarray = array('somekey' => 'value1''somekey2' => 'value2''somekey3' => 'value3');
    $fetch_value array_values($myarray);
    echo 
    $fetch_value[0]; // Outputs 'value1' 
    Imagination is more important than knowledge. - Einstein

  9. #9
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Maybe I wasn't clear. if You want to pull the first item from the array all you need is array_shift. You don't need to know the key or the values.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  10. #10
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,516
    Mentioned
    51 Post(s)
    Tagged
    1 Thread(s)
    Yeah, I guess that needed a little bit of explaination.

    Now it makes sense Thanks


    Is it possible to write that in a one-line statement, like this:

    PHP Code:
    <?= array_values($myarray)[0?>
    Or is that not correct

  11. #11
    Sesame Street Iimitk's Avatar
    Join Date
    Feb 2006
    Posts
    662
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The <?= ?> code writing method is obsolete and I strongly advice you against using it. You could do:
    PHP Code:
    $myarray = array('somekey' => 'value1''somekey2' => 'value2''somekey3' => 'value3');
    $myarray array_values($myarray);
    echo 
    $myarray[0]; // Outputs 'value1' 
    Imagination is more important than knowledge. - Einstein

  12. #12
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,516
    Mentioned
    51 Post(s)
    Tagged
    1 Thread(s)
    What are you supposed to use now instead of the <?= ?> tags?

  13. #13
    Sesame Street Iimitk's Avatar
    Join Date
    Feb 2006
    Posts
    662
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?php echo ...; ?>
    Imagination is more important than knowledge. - Einstein

  14. #14
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,516
    Mentioned
    51 Post(s)
    Tagged
    1 Thread(s)
    I thought that was the old way

    That's really the way you're supposed to do it in PHP5?

  15. #15
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And php 4... and php 3... and php 2...
    Me on StackOverflow | Blog & personal website.

    I mostly use: PHP, Java, JavaScript, Android.

  16. #16
    Sesame Street Iimitk's Avatar
    Join Date
    Feb 2006
    Posts
    662
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually it has nothing to do with PHP5. It's considered a best practice for writing code with compatibility and readability in mind. This is basically because the short output syntax <?= ?> is dependent on a directive setting in php.ini called short_open_tag. So if you happened to move your site to a host that has that directive disabled all your code will not function. Also when using XML with PHP, the short syntax would confuse the PHP parser as how to interpret <?xml ... ?>, unless you printed the XML starting code as a string to avoid confusion.
    Imagination is more important than knowledge. - Einstein

  17. #17
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The <?= ?> code writing method is obsolete
    While I personally don't use short tags, its not obsolete and I doubt it ever will be. If you look at the PHP6 spec, it states it's staying:
    Quote Originally Posted by PHP6 meeting minutes
    We kill "<&#37;" but keep "<?".
    Jani will prepare a patch that disallows mixing different open/close tags.
    We will not add "<?php =".
    However, it's recommended that you don't use it for maximum server compatibility.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


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
  •