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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:53:49+00:00 2026-06-10T12:53:49+00:00

Here is what I want to do: $newArray = array(); foreach($student as $s){ $newArray[$s->id][$s->grade]

  • 0

Here is what I want to do:

$newArray = array();

foreach($student as $s){
    $newArray[$s->id][$s->grade] = $s;
}

I want to sort the students by their grades (more of a group than a sort) but I just want the grades to be sorted not the id. I could have don’t this:

$newArray[$s->id] = $s->grade 
asort($newArray)

but I need the remaining data in $s. Also, there is huge chunk of data associated with each student which I want to maintain.

How can I achieve such a sorting?

  • 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-10T12:53:51+00:00Added an answer on June 10, 2026 at 12:53 pm

    Edit:

    Sine you’re working in a framework, best declare your sort callback as a member function (inside the same class as where you’ll be needing it, of course):

    private function sortCB(array $a, array $b)
    {//the array type hinting in arguments is optional
        $i = array_keys($a);//but highly recommended 
        $j = array_keys($b);
        if (end($i) === end($j))
        {
            return 0;
        }
        //replace '>' with '<' if you want to sort descending
        return (end($i) > end($j) ? 1 : -1);//this is ascending
    }
    

    Then, in the method where the actual sorting is needed:

    uasort($theArray,array($this,'sortCB'));
    

    For more examples, see the docs. I’ve added a full class example at the end of this (bulky) answer


    I’ve tried this on writecodeonline, which isn’t all too good at this kind of stuff, but this did work:

    $foo = array_fill_keys(array('foo','bar','q','Bond'),array());
    $i = '256';
    foreach($foo as $k=>$v)
    {
        $foo[$k][$i] = $k;
        $i = (string)((int)$i%2 === 0 ? ((int)$i/2)+1 : (int)$i*3);
    }
    function sortCB($a,$b)
    {
        $i = array_keys($a);
        $j = array_keys($b);
        if (end($i) === end($j))
        {
            return 0;
        }
        return (end($i) > end($j) ? 1 : -1);
    }
    uasort($foo,'sortCB');
    var_dump($foo);
    

    But since you’re using a framework, you might do well declaring that function as a member function private function sortCB(array $a,array $b), and use it like so:

    uasort($foo,array($this, 'sortCB'));
    

    There might be some more info on how best to use this callback function in a class context here


    Full example + usage (tested and working):

    class test
    {
        public $foo = null;
        public function __construct()
        {
            $this->foo = array_fill_keys(array('foo','bar','q','Bond'),array());
            $i = '256';
            foreach($this->foo as $k=>$v)
            {
                $this->foo[$k][$i] = $k;
                $i = (string)((int)$i%2 === 0 ? ((int)$i/2)+1 : (int)$i*3);
            }
        }
        private function sortCB($a,$b)
        {
            $i = array_keys($a);
            $j = array_keys($b);
            if (end($i) === end($j))
            {
                return 0;
            }
            return (end($i) > end($j) ? 1 : -1);
        }
        public function sortFoo()
        {
            uasort($this->foo,array($this,'sortCB'));
            print_r($this->foo);
            return $this->foo;
        }
    }
    $bar = new test();
    $arr = $bar->sortFoo();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

enter code here Want to remove items from a Main list , but give
I'm not entirely sure if I can do what I want here, but I
I want to sort a JSON object/array (shown below as myArray ), upon values
I want to sort a JSON object/array (shown below as myArray ), upon values
I want to filter an array by calculating the differences. Here's the code: public
Here i want to do UDP Socket programming in C. I have one device
All Here i want to run .sh file via system call in android NDK.
Need a little help with my jquery here I want all my button with
i m having following type of simple sql server table Here I want to
Please find the code at http://jsfiddle.net/wlogeshwaran/NGL8P/4/ Here i want to make the 'hi' ,

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.