/    Sign up×
Community /Pin to ProfileBookmark

I have a 2D array: $myarray[i][j]=x
I want to sort it by ‘i’. How do I do that?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 04.2005 — I think the [url=http://www.php.net/ksort]ksort()[/url] function is what you're looking for. (Or krsort() for reverse sort.)
Copy linkTweet thisAlerts:
@Louigi_VeronaauthorMay 04.2005 — You see - I am not even sure that it is a 2D array. It has one value but two indicies. ksort doesn't work. The reason that I'm using two indicies is because in my script I want to associate the value of the array with two database records. Second index can do that. But the sorting just doesn't work!
Copy linkTweet thisAlerts:
@scragarMay 04.2005 — [code=php]$myarray = array(array(1,2,3), array(4,5,6), array(7,8,9));
$tmparray = array();
for($i = 0; $i < count($myarray); $i++){
$tmparray[$i] = implode(";", $myarray[$i]);
};
sort($tmparray[$i]);
for($i = 0; $i < count($myarray); $i++){
$myarray[$i] = explode(";", $tmparray[$i]);
};[/code]
Copy linkTweet thisAlerts:
@Louigi_VeronaauthorMay 04.2005 — Thanks! ?
Copy linkTweet thisAlerts:
@scragarMay 04.2005 — your welcome.
×

Success!

Help @Louigi_Verona 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.28,
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,
)...