theITvideos Posted September 2, 2010 Share Posted September 2, 2010 Hi there! Ok I've a very simple code here: $character = array (name=>"Joe", occupation=>"Programmer", age=>30, "Learned language "=>"Java" ); print_r($character); echo "<br/>"; foreach ( $character as $key=>$val ){ print "$key = $val <br>"; } The output is: Array ( [name] => Joe [occupation] => Programmer [age] => 30 [Learned language ] => Java ) name = Joe occupation = Programmer age = 30 Learned language = Java Now instead of returning all the other elements, I just need to display only the first element in the array that is the value 'Joe'. I tried $val[name]; Output I get is not what I want: J P J All responses/feedbacks is always welcomed Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/212365-how-to-get-the-first-element-in-the-associative-array/ Share on other sites More sharing options...
wildteen88 Posted September 2, 2010 Share Posted September 2, 2010 To get the characters name, you'd use echo $character['name'] Or you can use array_shift. Eg $person = array_shif($character); echo $person; But that will remove the first element from the array. Quote Link to comment https://forums.phpfreaks.com/topic/212365-how-to-get-the-first-element-in-the-associative-array/#findComment-1106472 Share on other sites More sharing options...
theITvideos Posted September 2, 2010 Author Share Posted September 2, 2010 To get the characters name, you'd use echo $character['name'] Or you can use array_shift. Eg $person = array_shif($character); echo $person; But that will remove the first element from the array. Thanks the code you suggested: echo $character['name'] does output the first element but it repeats like 5 times: Joe Joe Joe Joe I just need 'Joe' to be displayed only once. Any suggestions bro? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/212365-how-to-get-the-first-element-in-the-associative-array/#findComment-1106477 Share on other sites More sharing options...
wildteen88 Posted September 2, 2010 Share Posted September 2, 2010 Huh? You using that snippet in in this loop? echo "<br/>"; foreach ( $character as $key=>$val ){ print "$key = $val <br>"; } You don't need the loop to echo out an item from an array. This is all you need $character = array (name=>"Joe", occupation=>"Programmer", age=>30, "Learned language "=>"Java" ); echo $character['name'] If $character was a multi-dimensional array (held more than one character) then you'd need to use the foreach loop. Quote Link to comment https://forums.phpfreaks.com/topic/212365-how-to-get-the-first-element-in-the-associative-array/#findComment-1106478 Share on other sites More sharing options...
Alex Posted September 2, 2010 Share Posted September 2, 2010 Don't forget that those keys should be strings, and as such they should be surrounded in quotes. That code will throw several undefined constant messages, but depending on the level your error_reporting is set to you might not see them. $character = array ("name"=>"Joe", "occupation"=>"Programmer", "age"=>30, "Learned language "=>"Java" ); Quote Link to comment https://forums.phpfreaks.com/topic/212365-how-to-get-the-first-element-in-the-associative-array/#findComment-1106500 Share on other sites More sharing options...
taquitosensei Posted September 2, 2010 Share Posted September 2, 2010 also if you're going to have more than one character it would look like this. $characters = array ( array ( "name"=>"Joe", "occupation"=>"Programmer", "age"=>30, "Learned language "=>"Java" ) ); foreach($characters as $character) { echo $character['name']; } Quote Link to comment https://forums.phpfreaks.com/topic/212365-how-to-get-the-first-element-in-the-associative-array/#findComment-1106503 Share on other sites More sharing options...
theITvideos Posted September 2, 2010 Author Share Posted September 2, 2010 also if you're going to have more than one character it would look like this. $characters = array ( array ( "name"=>"Joe", "occupation"=>"Programmer", "age"=>30, "Learned language "=>"Java" ) ); foreach($characters as $character) { echo $character['name']; } Thanks for the reply but this returns JPJ instead of 'Joe' Quote Link to comment https://forums.phpfreaks.com/topic/212365-how-to-get-the-first-element-in-the-associative-array/#findComment-1106512 Share on other sites More sharing options...
wildteen88 Posted September 2, 2010 Share Posted September 2, 2010 also if you're going to have more than one character it would look like this. $characters = array ( array ( "name"=>"Joe", "occupation"=>"Programmer", "age"=>30, "Learned language "=>"Java" ) ); foreach($characters as $character) { echo $character['name']; } Thanks for the reply but this returns JPJ instead of 'Joe' How? You sure you typing the code in properly. You should get Joe. Quote Link to comment https://forums.phpfreaks.com/topic/212365-how-to-get-the-first-element-in-the-associative-array/#findComment-1106526 Share on other sites More sharing options...
theITvideos Posted September 2, 2010 Author Share Posted September 2, 2010 also if you're going to have more than one character it would look like this. $characters = array ( array ( "name"=>"Joe", "occupation"=>"Programmer", "age"=>30, "Learned language "=>"Java" ) ); foreach($characters as $character) { echo $character['name']; } Thanks for the reply but this returns JPJ instead of 'Joe' How? You sure you typing the code in properly. You should get Joe. Yes bro I am now getting Joe I am a happy man! Quote Link to comment https://forums.phpfreaks.com/topic/212365-how-to-get-the-first-element-in-the-associative-array/#findComment-1106527 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.