How to get the first element in the Associative array

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

All is good. 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

Whats your suggestion?

Any responses/feedbacks is always welcome :slight_smile:

Thank you!

Hey guido2004 you are right. :slight_smile:

Actually I was thinking from the wrong side of the brain.

Ok I will describe a situation. By the way… one part of the equation is accomplished :slight_smile: Thanks to you for suggesting this simple $product[‘productId’]. Thats done.

Now I’ve a product webpage which has a listbox that has muliple profiles listed. One product can have single or multiple profiles assigned to it.

Once we select those profiles from the listbox and hit save, these profiles get assigned and saved in the products table under the profileID column separated by | that is

In product table
Profile ID
|66|71|84|

Once these Profile IDs is inserted in the Products table, I would like to update the related profiles with the productID inside Profile table so we know what Profile has these products under it.

So I do a quick for these profile IDs:

|66|71|84|

So I use an explode function to take out the ‘|’ (pipe)

$ProfileIdWithProds = explode("|", $product['shippingprofileID']);

Important: we need to insert the Product ID in each of these 3 Profile IDs:
66
71
84

So to loop thru these 3 ProfileID

foreach($ProfileIdWithProds as $ProfileProduct) 
{
//Now insert ProductID into related profiles in the Profile table 
}

Am I able to explain myself properly the scenario. Is this looping making some sense now.

The array is $character, not $val.
If you only want the first element of the array, there’s no need to loop through it.
You can use current, like Alex said, or

$character[‘name’];

Thanks for the reply but we are talking about associative array AND we are using a foreach loop and we need to get just the first element ‘Joe’

Any suggestions?

Assuming you haven’t iterated any of the elements, I think current() should work:

http://ca.php.net/manual/en/function.current.php

You may also want to look into reset()

Cheers,
Alex

Yes. Outside of the foreach-loop, just use $product[‘productId’] :slight_smile:

Well said. Now will share whats going on under the hood.

This functions returns an array and is stored in $product array:

$product = filter_products(array('productId' => $_VARS['productId'], 'supplierId' => $_SESSION['supplier_id'], 'enabled' => 1));

Then we do a little print_r() to see whats inside.

print_r($product)

I get the output:

Array ( [productId] => 28 [supplierId] => 66 [categoryId] => 14 [shippingprofileID] => 23 [productName] => testProductName )

Then I use a little looping (just bear with me) I get:

foreach($product as $prdprof => $ky) 
{
echo  "<br/>". $ky;
}

The output I get is:

28
66
14
23
testProductName 

So far so good. Now, I need to get just the ‘28’ value from this.

I hope now you get the picture :slight_smile:

Don’t use a simple example. Use a real world one.
You’re looping through a mysql query result set? Using a while loop? Then like I said, get rid of the foreach loop inside that while loop.

Yes thats a good point and this is just a simple example i wrote.

I have an associative array displaying data from a table (having multiple rows). And I am using foreach loop to iterate and display just the first element from the associative array. Can we use:


$character = array (name=>"Joe",
                    occupation=>"Programmer",
                    age=>30,
                    "Learned language "=>"Java"
);

echo "<br/>";
foreach ( $character as $key ){
    print "$key[name]";
} 

This still doesn’t display the first elements. Any workaround or suggestions?

Thanks bro! :slight_smile:

So?

AND we are using a foreach loop and we need to get just the first element ‘Joe’

Any suggestions?

Yes, don’t use the foreach loop. Why loop through an array if you only want the first element.