/    Sign up×
Community /Pin to ProfileBookmark

Multidim array sort

I have a 2d array and want to sort by a specific column.

So for example i have a table on a web page with headers – when clicking the headers it will sort all the data based on that column bu obviously keep all the other data the same so-
name no1. no2.
[0]b 5 7
[1]e 2 7
[2]s 8 1

so if no1. was clicked it would have

name no1. no2.
e 2 7
b 5 7
s 8 1

and possibly if no1. was clicked again it would sort in desc

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@96turnerriSep 24.2004 — always check the manual www.php.net

before posting here, nine times out of ten you will find your answer there ?

http://uk.php.net/manual/en/function.array-multisort.php
Copy linkTweet thisAlerts:
@kaibobbleauthorSep 24.2004 — but they don't seem to quite do what i want - i have tried a couple of them - one gave a memory error ?
Copy linkTweet thisAlerts:
@96turnerriSep 24.2004 — [code=php]
<?php
//make you arrays into three like below
$ar1 = array("10", 100, 100, "a"); //you char bit of array
$ar2 = array(1, 3, "2", 1); //1st numeric bit of array
$ar3 = array(1, 3, "2", 1); //2nd numeric bit of array
array_multisort($ar1, SORT_ASC, SORT_STRING, $ar2, SORT_NUMERIC, SORT_ASC, $ar3, SORT_NUMERIC, SORT_ASC);
?> [/code]


that should work never used this function before mind
Copy linkTweet thisAlerts:
@kaibobbleauthorSep 24.2004 — the thing i wasn't sure about that was the links have to be.. it will then reload the page with the sorted columns.

<ahref.......?col=name>name</a>

<ahref.......?col=no1>no1</a>

<ahref.......?col=no2>no2</a>


and then sort on $col

obviously you can't put in all the other values in array_multi..

becasue you don't know which has been clicked to sort on.
×

Success!

Help @kaibobble 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 5.20,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...