php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #54259 Add SORT_NONE type to array_mulitsort
Submitted: 2011-03-15 20:49 UTC Modified: 2021-02-21 04:22 UTC
Votes:2
Avg. Score:4.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: brad dot froehle at gmail dot com Assigned: cmb (profile)
Status: No Feedback Package: Arrays related
PHP Version: 5.3.5 OS: OS X 10.6
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: brad dot froehle at gmail dot com
New email:
PHP Version: OS:

 

 [2011-03-15 20:49 UTC] brad dot froehle at gmail dot com
Description:
------------
It'd be useful to have a SORT_NONE option in the array_multisort function.  This 
would allow users to specify that they do not care to sort along a particular 
index.

In the sample code below, I've attached one possible use arising in Drupal. (See 
http://drupal.org/node/972536).  Generically, we want to sort some array ($menu) 
before inserting it into the database.  To do so, we first compute the desired 
sort order ($sort) and then call the array_multisort function as
  array_multisort($sort, SORT_NUMERIC, $menu);

This works great, sorting $menu according to the sort order in $sort, however if 
$menu contains any objects the array_multisort function throws an error (Notice: 
Object of class stdClass could not be converted to int in ...).

I'm proposing the addition of a SORT_NONE option so that the function call could 
instead be
  array_multisort($sort, SORT_NUMERIC, $menu, SORT_NONE);
This would not throw an error since no comparisons would be done with the 
attributes of $menu.

Implementing this should be relatively straightforward, I would imagine.

Test script:
---------------
$sort = array(
  'A' => 2,
  'A' => 1,
  'B' => 1,
);
$menu['A'] = array(
  'access' => array(1),
);
$menu['B'] = array(
  'access' => array((object) array('a', 'b')),
);
$menu['C'] = array(
  'access' => array(0),
);

array_multisort($sort, SORT_NUMERIC, $menu, SORT_NONE);


Expected result:
----------------
$sort =
Array
(
    [C] => 1
    [B] => 1
    [A] => 2
)

$menu =
Array
(
    [C] => Array
        (
            [access] => Array
                (
                    [0] => 0
                )

        )

    [B] => Array
        (
            [access] => Array
                (
                    [0] => stdClass Object
                        (
                            [0] => a
                            [1] => b
                        )

                )

        )

    [A] => Array
        (
            [access] => Array
                (
                    [0] => 1
                )

        )

)


Actual result:
--------------
(Not applicable, since this is a feature request).

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-02-12 16:36 UTC] cmb@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2021-02-12 16:36 UTC] cmb@php.net
I fail to see why you couldn't use SORT_REGULAR[1].

[1] <https://3v4l.org/iFDlV>
 [2021-02-21 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Oct 17 12:03:34 2021 UTC