Jump to content
hungryfrank

getting the first value alphabetically

Recommended Posts

i have this code that enters the values of an array into my db

foreach ($results->CategoryArray->Category as $key => $value) { 
$categories_ids = (int) $value->CategoryID;
$CategoryName=$value->CategoryName;
if(strcmp($first_name,$CategoryName) <0)$firstID=(int) $value->CategoryID;
/// other codes to enter in db
}
echo $firstID

i want to get the value of $categories_ids when the $CategoryName is alphabetically first.  but it don't work. i always get the last value of the last one in the array.

 

Share this post


Link to post
Share on other sites

You're comparing with $first_name but you never update that value.

Share this post


Link to post
Share on other sites

Here's an alternative approach

$category = [   1 => 'kiwi',
                2 => 'lemon',
                3 => 'apple',
                4 => 'pear'     ];

asort($category);                            // sort them, preserving keys
list ($id, $cat) = each($category);          // get the first
echo "$id : $cat";                           // --> 3 : apple

Share this post


Link to post
Share on other sites

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.