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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:24:10+00:00 2026-05-14T01:24:10+00:00

Hallo, how can I pass more parameters to usort? I have different functions, which

  • 0

Hallo, how can I pass more parameters to usort?
I have different functions, which are very similar in structure, I want to have just one function:

<?php

     $arr = array(      array('number' => 100, 'string'=>'aaa'),
                array('number'=>50, 'string'=>'bdef'),
                array('number'=>150, 'string'=>'cbba')
            );


    usort($arr,  'sortNumberDesc');

    //How can I use just a single function?
    //How can I pass further parameters to usort?

    function sortNumberDesc($a, $b){
       $a = $a['number'];
       $b = $b['number'];
       if ($a == $b) return 0;
       return ($a > $b) ? -1 : +1;
    }

    function sortNumberAsc($a, $b){
       $a = $a['number'];
       $b = $b['number'];  
       if ($a == $b) return 0;
       return ($a < $b) ? -1 : +1;
    }

    //I want to do the same with just one function:
    //Sort ID is the search index, reverse DESC or ASC 
    function sort($a, $b, $sortId='number', $reverse = 0){
       $a = $a[$sortId];
       $b = $b[$sortId]; 
       if ($a == $b) return 0;
       if($reverse == false) return ($a > $b) ? -1 : +1;
       else return ($a < $b) ? -1 : +1;
    }

    print_r($arr);
?>
  • 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-14T01:24:11+00:00Added an answer on May 14, 2026 at 1:24 am

    You can use a much simpler function:

    function sortNumberAsc($a, $ b){
           $a = $a['number'];
           $b = $ b['number'];  
           if ($a == $b) return 0;
           return ($a < $b) ? -1 : +1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hallo! I want a blob object which I can pass around in python and
I want to pass a structure in C++ to a javascript function. I can
Hallo all. i want to create a .htaccess file if i type subdomain.domain.com can
Hallo. I have problems by setting the Eclipse envirorment, can anybody provide me a
Hello! My problem can be described the following way: I have some data which
I can pass multiple parameters and at last one block parameter to method. But
i Hallo, m using MDIContainer. 1. How can i pass the varaible from parent
Hello I have seen similar posts but none answer what I want to accomplish
Hallo, can someone tell me how to get clean url' s on the IIS
Hallo, How can I calculate a current time moment + exactly one week? The

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.