/    Sign up×
Community /Pin to ProfileBookmark

[code=php]
<?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);
?>
[/code]

[Output]
array(2) {
[0]=> array(5) {
[0]=> string(2) “10”
[1]=> int(100)
[2]=> int(100)
[3]=> int(11)
[4]=> string(1) “a”
}
[1]=> array(5) {
[0]=> int(1)
[1]=> int(3)
[2]=> string(1) “2”
[3]=> int(2)
[4]=> int(1)
}
}
[/Output]

I can’t figure out how mulitsort sort the arrays. It puzzles me that how the first array is sorted out and string “10” becomes the first element. Can anyone explain to me the way it works?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@SodbusterFeb 14.2009 — The SORT_STRING flag causes the items to be sorted by ASCII value.[code=php]$ar = array(
array("10", 11, 100, 100, "a", 2, 22, '2', '22'),
array( 1, 2, "2", 3, 1, 11, 22, '11', '22')
);
array_multisort($ar[0], SORT_ASC, SORT_STRING,
$ar[1], SORT_NUMERIC, SORT_DESC);
foreach ($ar[0] as $each) {
$each = (string) $each;
echo $each . ' => ' . ord($each[0]) . '<br />';[/code]
[CODE]10 => 49
100 => 49
100 => 49
11 => 49
2 => 50
2 => 50
22 => 50
22 => 50
a => 97
[/CODE]
Copy linkTweet thisAlerts:
@homer_j_simpsonauthorFeb 15.2009 — Thanks for your explanation. ? So basically it sorts array base on ascii value of first char of elements when used with SORT_STRING flag. But I still don't get the idea of how the second array is sorted. It doesn't look to me it is sorted in intended way which is descending. ?
×

Success!

Help @homer_j_simpson spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...