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!

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.

 

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

 

 

 

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.

 

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

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']; 
}

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

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.