php / php/doc-en

Strange behavior of array_multisort

Open
#1,474 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Category: Arrays Extension: standard
Dominant language
XML
Stars
596
Forks
890
Avg merge
1d 15h
Merged PRs (30d)
55

Description

Hello,

during investigation of PHP doc I found interesting thing in array_multisort function.

For argument rest there is written:

rest
More arrays, optionally followed by sort order and flags. Only elements corresponding to equivalent elements in previous arrays are compared. In other words, the sort is lexicographical.

Sort order and flags (in this direction), but in example 2 these parameters are switched. Switching should be allowed, but only if flags value is SORT_REGULAR, but is not this case

<?php
$ar = array(
       array("10", 11, 100, 100, "a"),
       array(   1,  2, "2",   3,   1)
      );
array_multisort($ar[0], SORT_ASC, SORT_STRING,
                $ar[1], SORT_NUMERIC, SORT_DESC);
var_dump($ar);
?>

EN DOC: https://www.php.net/manual/en/function.array-multisort.php
Code: https://github.com/php/php-src/blob/5d6bc25063e571da936010b1fae822f5d610deca/ext/standard/array.c#L5630

Live example: https://sandbox.onlinephpfunctions.com?s=s7EvyCjg5VJJLFKwVUgsKkqs1ODlUoAACFfJ0EBJR8HQEIgNDKCEUqKSpg6aOiADqEbBCChrBNSgoGAMIgw1Yco0rXm5wCrjc0tzSjKL84tKNIDWRhvE6igE-weFxDsGO0NZwSFBnn7uCAvgAKTeEKbeL9TXNcgTpsfFNdgZZEVZYlF8SmluAchsEN_eDgA%2C&v=7.3.5

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked English manual page, especially the array_multisort description and example 2, then compare its parameter order with ext/standard/array.c around line 5630. Verify the documented ordering and restrictions against the linked PHP example. Done means the documentation accurately explains the accepted sort order and flags and no longer contradicts the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.