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

  • Home
  • SEARCH
  • 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 8021645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:02:53+00:00 2026-06-04T22:02:53+00:00

I have never used anonymous functions in PHP before, but I found a piece

  • 0

I have never used anonymous functions in PHP before, but I found a piece of code that uses one in order to sort objects

usort($numTurnsPerUser,build_sorter('turns'));

function build_sorter($key) {
    return function ($a, $b) use ($key) {
        return strnatcmp($a[$key], $b[$key]);
    };
}

This code will sort an object by a key (I pass in “turns”). For example, an object that looks like this: (written in JSON, just for readability)

$numTurnsPerUser = {
    "31":{
        "turns":15,
        "order":0
    }, "36":{
        "turns":12, 
        "order":1
    }, "37":{
        "turns":14, 
        "order":2
    }
}

will be sorted into an object that looks like this:

$numTurnsPerUser = {
    "36":{
        "turns":12,
        "order":1
    }, "37":{
        "turns":14,
        "order":2
    }, "31":{
        "turns":15, 
        "order":0
    }
}

This worked great on my local server, which is running PHP 5.3.0, but it fails to work on my online server, which is running “php5” – I’m unable to find any information other than that. I am getting an error

Parse error: syntax error, unexpected T_FUNCTION

I read that PHP < 5.3 cannot use anonymous functions and must use create_function, but the “use” part of the anonymous function has me stumped. Could someone please explain to me what that “use” part of the function is, or better yet, how I can “translate” this to the create_function parameters needed?

  • 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-04T22:02:55+00:00Added an answer on June 4, 2026 at 10:02 pm

    You could do like this:

    Class Sorter {
      private $key;
    
      public function __construct($key) {
        $this->key = $key;
      }
    
      public function sort($a, $b) {
        return strnatcmp($a[$this->key], $b[$this->key]);
      }
    }
    
    usort($numTurnsPerUser, array(new Sorter('key_b'), 'sort'));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have never used Try-catch in my code before, but now I need to
I have never used CRON or anything like that, rails etc.. before, but I
Just trying to understand that - I have never used it before. How is
I have never used PHP with CLI, but I have seen scripts running with
I have never used Informix before and I'm trying to write a query that
I have never used namespaces for my code before. (Other than for using STL
I have never used php but have been asked to set up a site
I have never used Windsor before but have used other DI frameworks, and I
This probably something really simple, but I have never used CAShapeLayers before, I imported
I have never used Prototype before. But now when I'm using Rails, it seems

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.