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 7845431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:13:13+00:00 2026-06-02T17:13:13+00:00

Trying to sort by this multidimensional array by screen name: //Looping through this for

  • 0

Trying to sort by this multidimensional array by screen name:

//Looping through this for each member of a group
unset($member_info);
$member_info->id           =   $member->id;
$member_info->screen_name  =   $member->screen_name;
$member_info->first_name   =   $member->first_name;
$member_info->last_name    =   $member->last_name;
$member_info->email        =   $member->email;

//Sort member_info by screen name
$member_array = sort_by_array_key($member_info,$member_info->screen_name,'ASC');
$member_list[] = $member_array;

Sort function:

function sort_by_array_key($array,$sort_key,$dir = 'ASC') {
    usort($array, function($a,$b) use ($sort_key){
        return strnatcasecmp($a["$sort_key"], $b["$sort_key"]);
    });
    if ( $dir == 'DESC' ) { return array_reverse($array); }
    else { return $array; }
}

As an example, screen names are currently in this order even after I run the sort function:

newUser
Chris
Carlos
Jason

I am probably applying the sort at the wrong time but after looking at other similar examples here I was having trouble with this, any help is appreciated, thanks!

  • 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-02T17:13:14+00:00Added an answer on June 2, 2026 at 5:13 pm

    First of all, you’re sorting an individual item there, where you probably want to sort the actual list, $member_list. Also, you’re trying to sort an array where you initialize an object. Either initialize an array or sort an object.

    If I assume you really want to play with arrays, like you imply on the subject, this might be something you’re trying to do:

    <?php
    
    $member_info = Array();
    $member_info['id']           =   '1';
    $member_info['screen_name']  =   'Chris';
    $member_info['first_name']   =   '';
    $member_info['last_name']    =   '';
    $member_info['email']        =   '';
    
    $member_list[] = $member_info;
    
    $member_info = Array();
    $member_info['id']           =   '2';
    $member_info['screen_name']  =   'Carlos';
    $member_info['first_name']   =   '';
    $member_info['last_name']    =   '';
    $member_info['email']        =   '';
    
    $member_list[] = $member_info;
    
    $member_info = Array();
    $member_info['id']           =   '3';
    $member_info['screen_name']  =   'Jason';
    $member_info['first_name']   =   '';
    $member_info['last_name']    =   '';
    $member_info['email']        =   '';
    
    $member_list[] = $member_info;
    
    
    //Sort member_list by screen name
    $member_list = sort_by_array_key($member_list,'screen_name','ASC');
    
    function sort_by_array_key($array,$sort_key,$dir = 'ASC') {
        usort($array, function($a,$b) use ($sort_key){
            return strnatcasecmp($a["$sort_key"], $b["$sort_key"]);
        });
        if ( $dir == 'DESC' ) { return array_reverse($array); }
        else { return $array; }
    }
    
    print_r($member_list);
    
    /*
    Array
    (
        [0] => Array
            (
                [id] => 2
                [screen_name] => Carlos
                [first_name] =>
                [last_name] =>
                [email] =>
            )
    
        [1] => Array
            (
                [id] => 1
                [screen_name] => Chris
                [first_name] =>
                [last_name] =>
                [email] =>
            )
    
        [2] => Array
            (
                [id] => 3
                [screen_name] => Jason
                [first_name] =>
                [last_name] =>
                [email] =>
            )
    
    )
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to sort this array of objects according to (1) depth and (2) weight,
I'm trying to sort a multidimensional array by multiple keys, and I have no
Possible Duplicate: Sort multidimensional array by multiple keys I am trying to find some
I'm trying to sort an array that would look something like this: (please ignore
I'm trying to sort an array of people objects. The class looks like this.
I am trying to sort this multi-dimensional array by rating. Array ( [0] =>
I am trying to sort a multidimensional JSON array (called jsontest here) using JQuery
I'm trying to sort a array of this kind : array ( 0 =>
iam trying to sort this array by array[key]['premium']['Monthly'] and if there are two Monthly
Trying to do this sort of thing... WHERE username LIKE '%$str%' ...but using bound

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.