note 129677 added to function.array-multisort
| From: | Anonymous | Date: | Tue, 30 Jul 2024 16:49:02 +0000 |
| Subject: | note 129677 added to function.array-multisort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221450@lists.php.net to get a copy of this message | ||
Arrays in PHP are "ordered maps". The "order" is internal, and has nothing to
do with the numerical values of the keys.
Array_multisort() seems to sort based on this internal order. It DOES NOT maintain numeric key
association, and then renumber based on this association. It maintains internal order association,
and numeric keys are renumbered based on this association. String-keys do not maintain key
association or internal order association, at least when mixed with numeric keys.
In other words, your 2 arrays must be built similarly or perhaps unexpected results will be
obtained.
<?php
$foo=array("foo"=>"fazz", 7, 5,"sing"=>"soft", 9, 2,
0,"fing"=>"fong", 8, 1, 4);
$bar=array("bar"=>"bazz",17,45,"sing"=>"loud",29,72,90,"bing"=>"bong",38,81,54);
array_multisort($foo, $bar);
echo
var_dump($foo),"<br><br>",var_dump($bar),"<br><br>";
?>
array(11) { [0]=> int(0) [1]=> int(1) [2]=> int(2) [3]=> int(4) [4]=> int(5) [5]=>
int(7) [6]=> int(8) [7]=> int(9) ["foo"]=> string(4) "fazz"
["fing"]=> string(4) "fong" ["sing"]=> string(4)
"soft" }
array(11) { [0]=> int(90) [1]=> int(81) [2]=> int(72) [3]=> int(54) [4]=> int(45)
[5]=> int(17) [6]=> int(38) [7]=> int(29) ["bar"]=> string(4) "bazz"
["bing"]=> string(4) "bong" ["sing"]=> string(4)
"loud" }
<?php
$foo=array("foo"=>"fazz", 7, 5,"sing"=>"soft", 9, 2,
0,"fing"=>"fong", 8, 1, 4);
$bar=array(17,45,29,72,90,"sing"=>"loud",38,81,54,"bing"=>"bong","bar"=>"bazz");
array_multisort($foo, $bar);
echo
var_dump($foo),"<br><br>",var_dump($bar),"<br><br>";
?>
array(11) { [0]=> int(0) [1]=> int(1) [2]=> int(2) [3]=> int(4) [4]=> int(5) [5]=>
int(7) [6]=> int(8) [7]=> int(9) ["foo"]=> string(4) "fazz"
["fing"]=> string(4) "fong" ["sing"]=> string(4)
"soft" }
array(11) { [0]=> int(38) ["bar"]=> string(4) "bazz"
["sing"]=> string(4) "loud" ["bing"]=> string(4)
"bong" [1]=> int(29) [2]=> int(45) [3]=> int(54) [4]=> int(90) [5]=>
int(17) [6]=> int(81) [7]=> int(72) }
My Arrays in my project were built using the <?php $foo[]=$filename ?> syntax in a loop, with
a string-keyed property added in at a random position within the loop to store directory names if
they were found, while a matching array was built with the complete file-paths, but no directory
info. Can't use Array_multisort to sort both arrays by the filenames, even after adding a
"fake" matching string-keyed property to the array of filepaths after-the-fact.
I had to build my array of filepaths in a separate variable, sort, then add it to the filenames
array as a string-keyed property after the fact.
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 2600:100e:b07a:c67e:8c26:9044:6381:24b7)
----
Manual Page -- https://php.net/manual/en/function.array-multisort.php
Edit -- https://main.php.net/note/edit/129677
Del: integrated -- https://main.php.net/note/delete/129677/integrated
Del: useless -- https://main.php.net/note/delete/129677/useless
Del: bad code -- https://main.php.net/note/delete/129677/bad+code
Del: spam -- https://main.php.net/note/delete/129677/spam
Del: non-english -- https://main.php.net/note/delete/129677/non-english
Del: in docs -- https://main.php.net/note/delete/129677/in+docs
Del: other reasons-- https://main.php.net/note/delete/129677
Reject -- https://main.php.net/note/reject/129677
Search -- https://main.php.net/manage/user-notes.php