Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8354911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:42:08+00:00 2026-06-09T09:42:08+00:00

This question is followed up question from ‘This question’ . The codeigniter code returns

  • 0

This question is followed up question from ‘This question’. The codeigniter code returns the database recordset as following array.

Array
(
    [0] => stdClass Object
    (
        [user_id] => NLK32439
        [first_name] => sdn
        [last_name] => hf]zL
        [email_address] => user1@hotmail.com
        [mobile_number] => 9841349349
        [description] => g]kfn
        [date_joined] => 09-AUG-12
        [status] => 1
        [username] => user1
        [userpassword] => 691f9298642af07c2d6ea8fef56074201e077b34
    )

    [1] => stdClass Object
    (
        [user_id] => NLK94358
        [first_name] => alag
        [last_name] => k|wfg
        [email_address] => user2@gmail.com
        [mobile_number] => 823472384723
        [description] => g]kfn
        [date_joined] => 09-AUG-12
        [status] => 1
        [username] => user2
        [userpassword] => 691f9298642af07c2d6ea8fef56074201e077b34
    )

    [2] => stdClass Object
    (
        [user_id] => NLK32437
        [first_name] => ;lag
        [last_name] => %]qL
        [email_address] => user3@msn.com
        [mobile_number] => 9851112412
        [description] => g]kfn
        [date_joined] => 08-AUG-12
        [status] => 1
        [username] => user3
        [userpassword] => 691f9298642af07c2d6ea8fef56074201e077b34
    )

    [3] => stdClass Object
    (
        [user_id] => NLK32435
        [first_name] => clgn
        [last_name] => zdf{
        [email_address] => user4@msn.com
        [mobile_number] => 984134354
        [description] => g]kfn
        [date_joined] => 08-AUG-12
        [status] => 1
        [username] => user4
        [userpassword] => 0e025eade868b4b481f41ff7449bc1967261e170
    )

)

All i want to do is sort array by “first_name” on the basis of certain condition. My php code so far is-

<?php
usort($array, function ($a, $b) {
    static $order = array('c', 's','a',';', 'L');
    return array_search($a->first_name, $order) - array_search($b->first_name, $order);
});
?>

My intention to do is sorting the array so that the first_name begining with ‘c’ comes first, with ‘s’ comes second, with ‘a’ comes third and with ‘;’ comes fourth.

But above code is not working as expected. It is returning the records in the sequence :

's','a',';','c'

it should return in the sequence

'c', 's','a',';'

I m using custom Devnagari font, so each albhabet in keyboard represent certain character in devnagari. So in devnagari, c comes first, s second and so on.

Any help will be highly appreciated. Thanks a ton in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:42:10+00:00Added an answer on June 9, 2026 at 9:42 am

    This because using array_search will only return when the string is completely match some string in the array.

    $array = array('a','b','c');
    var_dump(array_search('blue',$array))
    // This will output
    bool(false)
    

    So, if you want you can try this :

    <?php
    usort($array, function ($a, $b) {
    static $order = array('c', 's','a',';', 'L');
    return array_search(substr($a->first_name,0,1), $order) - array_search(substr($b->first_name,0,1), $order);
    });
    ?>
    

    Hope this helps.

    UPDATE : for your question on your comment, I have an idea. Please take alook.

    usort($array, function($a, $b) {
        //example for the orders
        static $orders = array (';l', 'c', 's', 'a', ';', 'L');
    
        //variables to count the point for $a and $b (it is equal at first)
        $pointA = 1;
        $pointB = 1;
    
        //also variables to mark if $a or $b are matched on one of the orders
        $isFoundA = false;
        $isFoundB = false;
    
        //iterate foreach orders
        $i = 0; //this is something to add to the point, the more earlier the first_name found in the order, the less point will be added
        foreach ($orders as $order) {
            //if $a->first_name is still not found in orders and it has been just founded 
            if (!$isFoundA && strpos($a->first_name,$order) === 0) {
                $pointA += $i;
                $isFoundA = true; //we don't need to check this again since we had found it
            }
            //if $b->first_name is still not found in orders and it has been just founded 
            if (!isFoundB && strpos($b->first_name,$order) === 0) {
                $pointB += $i;
                $isFoundB = true; //the same as above, we don't need to check it again
            }
            $i++;
        }
        //after iterate in orders, we check the points for $a and $b
        if ($pointA == $pointB) return 0;
        else return ($pointA < $pointB) ? -1 : 1;
    });
    

    Now the order has no limitation on characters. You can specify something like static $orders = array('abc','a',';'); or static $orders = array('abcdef', 'b', 'z', ';', 'c['); Please let me know if it works on you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've followed instructions from this question on SO to set up RVM, rubies and
I posted this question and have a freshly minted code signing cert from Thawte.
I have followed the instructions from this SO question: How to change the text
I've followed the steps from this question: Higher color depth for MFC toolbar icons?
This question stems from this question that I asked yesterday. I've followed Theo's advice
This question is regarding getting version directly from assembly. I have followed instructions from
I followed the advice of this question: Converting Storyboard from iPhone to iPad and
Followed this question about delayed_job and monit Its working on my development machine. But
I have followed the suggestion in this question as I am using Django, I
This question is related to another question I wrote: Trouble using DOTNET from PHP.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.