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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:37:42+00:00 2026-06-13T05:37:42+00:00

I have a class that is basically a wrapper to an Array and implements

  • 0

I have a class that is basically a wrapper to an Array and implements IteratorAggregate.
When a new object of the class is created, it stores it’s value in a protected variable called $value. The value can be a string, integer, etc.. or a traversable object (like an array).

This object is “recursive” since when a traversable object (like an array) is passed to the constructor, he creates another instance of the class. Here’s the constructor to clarify:

class ListItem implements \IteratorAggregate
{
    protected $readOnly = false;
    protected $key;
    protected $value;

    public function __construct($key, $value, $readOnly = false)
    {
        if($readOnly) $this->readOnly = true;
        if(is_numeric($key)) $key = 'index'.$key;
        $this->key = $key;

        if (is_array($value) || $value instanceof Traversable) {
            $this->value = array();
            foreach($value as $k => $v) {
                if(is_numeric($k)) $k = 'index'.$k;
                $this->value[$k] =  new ListItem($k, $v, $readOnly);
            }
        } else {
            $this->value = $value;
        }
    }

    public function __toString()
    {
        if ( is_array($this->value) ) {
            return 'ListItem OBJECT(' . count($this->value) . ')';
        } else {
            return $this->value;
        }
    }

Right now, I’m trying to write a simple sorting method for the class.

To sort by Key, this works like a charm:

$success = ksort($this->value, $comparison);

but to sort by value, asort does not work since the actual value I’m trying to sort is stored inside the value property.

So I tried using uasort, like this:

   $success = uasort($this->value, function ($a, $b)
   {                    
      if ($a->value == $b->value) return 0;
      else if($a->value < $b->value) return -1;
      else return 1;
   });

but for some unclear reason i get the following error:

Warning: uasort() [function.uasort]: Array was modified by the user
comparison function in /* /* /* /ListItem.php on line 129


Q. Why does this happen if I’m just accessing $value for comparison not actually changing anything?

  • 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-13T05:37:43+00:00Added an answer on June 13, 2026 at 5:37 am

    It seems a closure (or anonymous function) is in the global scope which means uasort could not access private or protected members of the ListItem object (although other ListItem Objects can access their sibling’s private/protected properties)

    this solved the problem: (casting to string)

    $success = uasort($this->value, function ($objA, $objB) use ($comparison)
    {
        $a = (string) $objA;
        $b = (string) $objB;
        if($comparison == ListItem::SORT_NUMERIC) {
            if (is_numeric($a)) $a = (int) $a;
            if (is_numeric($b)) $b = (int) $b;
        }                   
        if ($a == $b) return 0;
        else if($a < $b) return -1;
        else return 1;
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that basically stores files in amazon s3. Here is what
I have a class that is its own activity that basically i use to
I have a static methods class, Utils, that is basically for utility methods, its
I'm testing a C++ class with a number of functions that all have basically
I have a Sinatra app that, boiled down, looks basically like this: class MyApp
I have a derived class that's a very thin wrapper around a base class.
So here's the situation : I've created a SetWritable class, basically a wrapper for
I have class that extend FragmentActivity in it I add fragment to layout as
I have class that looks like this: class A { public: class variables_map vm
I have class that represents users. Users are divided into two groups with different

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.