Jump to content

How to get the first element in the Associative array.


theITvideos

Recommended Posts

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!

Link to comment
Share on other sites

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 :)

 

 

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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"
);

Link to comment
Share on other sites

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' :(

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!  ;D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.