[PHP] Array sorteren - 2 arrays

Gestart doorOne2xs3 reacties11 weergaven

4 berichten · pagina 1 van 1

One2xs

Bot · Topicstarter

Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Hoi,

Ik heb 2 arrays en daar heb ik er 1 van gemaakt met array_merge

$var = array_merge(Glybe::GetOnlineUsersAsArray(), Glybe::GetOnlineGuestsAsArray());


Dat zijn 2 functions die ongeveer gelijk zijn, alleen haalt hij data uit verschillende tabellen, hij geeft ook een last_active mee, dat is een datetime datum (NOW() in MySQL) en ik wil graag dat hij die 2 verschillende arrays bij elkaar voegt maar niet los van elkaar (Want nu worden eerst online users weergegeven en daarna online gasten) maar ik wil dus dat ze door elkaar kunnen staan

Hoe moet ik dit oplossen?

Groet,
Jesse

toevoeging op 3 mei 2012, 16:48:08:
http://php.net/manual/en/array.sorting.php die functions werken allemaal niet, hij sorteert wel, maar per array apart ook al doe ik dan function($var); waar die arrays dus al samen gevoegd zijn sadley
0
Berichten
3.283
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
http://www.php.net/manual/en/function.array-multisort.php

en heb je deze nodig:

Quote:
The data as an array, called data. This would usually, for example, be obtained by looping with mysql_fetch_assoc().

[COLOR= #0000BB]<?php
$data[/COLOR][COLOR= #007700][] =[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]volume[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #0000BB]67[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]edition[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #0000BB]2[/COLOR][COLOR= #007700]);[/COLOR]
[COLOR= #0000BB]$data[/COLOR][COLOR= #007700][] =[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]volume[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #0000BB]86[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]edition[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #0000BB]1[/COLOR][COLOR= #007700]);[/COLOR]
[COLOR= #0000BB]$data[/COLOR][COLOR= #007700][] =[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]volume[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #0000BB]85[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]edition[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #0000BB]6[/COLOR][COLOR= #007700]);[/COLOR]
[COLOR= #0000BB]$data[/COLOR][COLOR= #007700][] =[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]volume[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #0000BB]98[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]edition[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #0000BB]2[/COLOR][COLOR= #007700]);[/COLOR]
[COLOR= #0000BB]$data[/COLOR][COLOR= #007700][] =[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]volume[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #0000BB]86[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]edition[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #0000BB]6[/COLOR][COLOR= #007700]);[/COLOR]
[COLOR= #0000BB]$data[/COLOR][COLOR= #007700][] =[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]volume[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #0000BB]67[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #DD0000]edition[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #0000BB]7[/COLOR][COLOR= #007700]);[/COLOR]
?>


We have an array of rows, but array_multisort() requires an array of columns, so we use the below code to obtain the columns, then perform the sorting.

[COLOR= #0000BB]<?php[/COLOR]
[COLOR= #FF8000]// Obtain a list of columns[/COLOR]
[URL="http://www.php.net/foreach"]foreach[/URL] [COLOR= #007700]([/COLOR][COLOR= #0000BB]$data[/COLOR] [COLOR= #007700]as[/COLOR] [COLOR= #0000BB]$key[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #0000BB]$row[/COLOR][COLOR= #007700]) {[/COLOR]
    [COLOR= #0000BB]$volume[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]$key[/COLOR][COLOR= #007700]]  =[/COLOR] [COLOR= #0000BB]$row[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]volume[/COLOR][COLOR= #007700]];[/COLOR]
    [COLOR= #0000BB]$edition[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]$key[/COLOR][COLOR= #007700]] =[/COLOR] [COLOR= #0000BB]$row[/COLOR][COLOR= #007700][[/COLOR][COLOR= #DD0000]edition[/COLOR][COLOR= #007700]];
}[/COLOR]

[COLOR= #FF8000]// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key[/COLOR]
[URL="http://www.php.net/array_multisort"]array_multisort[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$volume[/COLOR][COLOR= #007700],[/COLOR] SORT_DESC[COLOR= #007700],[/COLOR] [COLOR= #0000BB]$edition[/COLOR][COLOR= #007700],[/COLOR] SORT_ASC[COLOR= #007700],[/COLOR] [COLOR= #0000BB]$data[/COLOR][COLOR= #007700]);[/COLOR]
?>
0
Berichten
2.692
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Het is al niet meer nodig maar toch bedankt.. kunnen we nog wel gebruiken biggrin
0
One2xs

Bot · Topicstarter

Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Is inderdaad al opgelost biggrinGewoon alles toch maar in 1 tabel gepropt emo.
0

Jouw reactie

Deel je idee, stel een vraag of help een ander verder.

Editor laden…

Praat mee met de One2xs-community.

Inloggen om te reageren