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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:19:12+00:00 2026-05-18T05:19:12+00:00

This code does not run properly, but it suggests what I am trying to

  • 0

This code does not run properly, but it suggests what I am trying to do:

function sort_2d_by_index($a,$i) {
  function cmp($x, $y) {
    // Nested function, can't find $i
    // (global $i defeats the purpose of passing an arg)
    if ($x[$i] == $y[$i]) { return 0; }
    return ($x[$i] < $y[$i]) ? -1 : 1;
  }

  usort($a,"cmp");
  return $a;
}

There HAS to be a much better way to do this. I’ve been examining ksort(), multisort(), and all sorts of sorts until I’m sort of tired trying to sort it all out.

The situation is this: I’ve got a 2-d array…

array(
  array(3,5,7),
  array(2,6,8),
  array(1,4,9)
);

…and I want to sort by a column index. Say, column [1], would give this result:

array(
  array(1,4,9),
  array(3,5,7),
  array(2,6,8)
);

Does someone have a link (I’m sure this has been asked before), or could someone say “you need foosort, definitely”. Thanks very much.

  • 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-05-18T05:19:13+00:00Added an answer on May 18, 2026 at 5:19 am

    In the documentation of array_multisort it is mentioned that it can be used for this kind of thing.

    You can’t avoid creating an array that consists of only one column:

    $sort_column = array();
    foreach ($a as $row)
        $sort_column []= $row[1]; // 1 = your example
    
    array_multisort($sort_column, $a);
    

    This sorts both arrays synchronously so that afterwards your whole array is sorted in the same order as the $sort_column array is.

    As of PHP 5.3 you can use a closure (pass $i into the function) by defining your cmp function like this:

    $cmp = function($x, $y) use ($i) { ... };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code does not throw an error but the query fails, that is, the
This code does not function as expected: // $field contains the name of a
I'm newbie in Python. I can't understand why this code does not work: reOptions
foo\r\nbar.replace(/(foo).+/m, bar) Hello. I can not understand why this code does not replace foo
This javascript code does not work in IE8, but works in Firefox and Google
Why this code does not compile (Cygwin)? #include <vector> template <class Ttile> class Tilemap
Could someone tell me why this code does not update the data response? It's
For some reason, this code does not actually draw my bitmap file... or show
I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
I'm not really getting how this code does what it does: char shellcode[] =

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.