hungryfrank Posted December 23, 2016 Share Posted December 23, 2016 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted December 23, 2016 Share Posted December 23, 2016 You're comparing with $first_name but you never update that value. Quote Link to comment Share on other sites More sharing options...
Barand Posted December 23, 2016 Share Posted December 23, 2016 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 Quote Link to comment 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.