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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:17:20+00:00 2026-06-15T02:17:20+00:00

I’ve found some solutions to a sorting problem I’ve been having, but the code

  • 0

I’ve found some solutions to a sorting problem I’ve been having, but the code uses anonymous functions in PHP. Im using version 5.2.17 and I believe anonymous functions are not supported.

How would I change the following blocks of code so I can use them in PHP 5.2.17

$keys = array_flip($order);

usort($items, function($a, $b) use($keys)
{
    return $keys[$a['id']] - $keys[$b['id']];
});

from PHP sort multidimensional array by other array

And

$sorted = array_map(function($v) use ($data) {
    return $data[$v - 1];
}, $order);

from PHP – Sort multi-dimensional array by another array

UPDATE:
One of the problems is I’m not sure how the variables $a, $b and $v are used. So I’m not sure how to create normal functions, thus bypassing the anon functions.

  • 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-15T02:17:21+00:00Added an answer on June 15, 2026 at 2:17 am

    Both of the anonymous functions make use of the use clause to pass variables into the local scope.

    You can achieve the same with object methods in which the objects have those variables as properties.

    Inside the object method you then can access these.

    $sorted = array_map(function($v) use ($data) {
        return $data[$v - 1];
    }, $order);
    

    An exemplary mapping object then could look like:

    class MapObj
    {
        private $data;
        public function __construct($data) {
            $this->data = $data;
        }
    
        public function callback($v) {
            return $this->data[$v - 1];
        }
    }
    

    As you can see it has the same functionality but just written in PHP 5.2 syntax.

    And it’s usage:

    $map = new MapObj($data);
    $callback = array($map, 'callback');
    $sorted = array_map($callback, $order);
    

    And that’s how it works. Callbacks for object methods are always written in form of an array with two members, the first one is the object instance, and the second one is the name of the object method.

    Naturally you can extend this an put the mapping function into the mapping object, so it’s more straight forward:

    class MapObj
    {
        ...
    
        public function map(array $order) {
            $callback = array($this, 'callback');
            return array_map($callback, $order);
        }
    }
    

    New usage:

    $map = new MapObj($data);
    $sorted = $map->map($order);
    

    As you can see this might make the usage a bit more straight forward. I must admit, my method naming is not really brilliant here, so I leave some room for your improvements.

    Another benefit is, you can make the visibility of the callback method private then.


    The situation with passing the data to work with in the callback as a parameter to the mapping function. That is because you wrote you already have a class that you want to make use of, but you can not touch the constructor. So the given example is a bit short.

    Here is another example without using the constructor, I removed it:

    class MyObj
    {
        private $data;
    
        private function callback($v) {
            return $this->data[$v - 1];
        }
    
        public function map($order, $data) {
            $callback = array($this, 'callback');
            $this->data = $data;
            return array_map($callback, $order);
        }
    }
    

    As you can see, the constructor is not needed any longer to pass the $data, but instead it’s just passed into the map() method as an additional parameter. Usage:

    $myObj = new MyObj(....); // somewhere.
    
    // later on:
    
    $myObj->map($order, $data);
    
    // could be also:
    
    $this->map($order, $data);
    

    As you can see, how you set the private member variable is up to you. Do what fits the job.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I have been unable to fix a problem with Java Unicode and encoding. The
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

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.