Array sorteren op "Score" en dat in een variable stoppen.
Dit is de array. ;)
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Array
(
[0] => SimpleXMLElement Object
(
[Username] => robin
[ID] => 1
[Score] => 3491
[Died] => 13
)
[1] => SimpleXMLElement Object
(
[Username] => name
[ID] => 418
[Score] => 131
[Died] => 914
)
[2] => SimpleXMLElement Object
(
[Username] => player
[ID] => 852
[Score] => 6174
[Died] => 12
)
)
(
[0] => SimpleXMLElement Object
(
[Username] => robin
[ID] => 1
[Score] => 3491
[Died] => 13
)
[1] => SimpleXMLElement Object
(
[Username] => name
[ID] => 418
[Score] => 131
[Died] => 914
)
[2] => SimpleXMLElement Object
(
[Username] => player
[ID] => 852
[Score] => 6174
[Died] => 12
)
)
maar ik wil dat die gesorteerd word op Score dus ik wil dat er dan dit weer uitkomt in de variable $user_desc
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Array
(
[0] => SimpleXMLElement Object
(
[Username] => player
[ID] => 852
[Score] => 6174
[Died] => 12
)
[1] => SimpleXMLElement Object
(
[Username] => robin
[ID] => 1
[Score] => 3491
[Died] => 13
)
[2] => SimpleXMLElement Object
(
[Username] => name
[ID] => 418
[Score] => 131
[Died] => 914
)
)
(
[0] => SimpleXMLElement Object
(
[Username] => player
[ID] => 852
[Score] => 6174
[Died] => 12
)
[1] => SimpleXMLElement Object
(
[Username] => robin
[ID] => 1
[Score] => 3491
[Died] => 13
)
[2] => SimpleXMLElement Object
(
[Username] => name
[ID] => 418
[Score] => 131
[Died] => 914
)
)
en dan wil ik dat er dit uitkomt in de variable $user_asc
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Array
(
[0] => SimpleXMLElement Object
(
[Username] => name
[ID] => 418
[Score] => 131
[Died] => 914
)
[1 => SimpleXMLElement Object
(
[Username] => robin
[ID] => 1
[Score] => 3491
[Died] => 13
)
[2] => SimpleXMLElement Object
(
[Username] => player
[ID] => 852
[Score] => 6174
[Died] => 12
)
)
(
[0] => SimpleXMLElement Object
(
[Username] => name
[ID] => 418
[Score] => 131
[Died] => 914
)
[1 => SimpleXMLElement Object
(
[Username] => robin
[ID] => 1
[Score] => 3491
[Died] => 13
)
[2] => SimpleXMLElement Object
(
[Username] => player
[ID] => 852
[Score] => 6174
[Died] => 12
)
)
Dus mijn vraag is hoe ik dit kan doen?
Ik heb al gezocht op internet maar ik kon nergens iets vinden.
Ik denk zelf dat ik dit met asort moet doen.
Gewijzigd op 23/07/2010 08:47:15 door Robin van der Vliet
Ik denk zelf zo iets:
Code (php)
1
2
3
4
5
6
2
3
4
5
6
<?php
$user_desc = array_multisort($user, "Score", SORT_NUMERIC, SORT_DESC)
$user_asc = array_multisort($user, "Score", SORT_NUMERIC, SORT_ASC)
?>
$user_desc = array_multisort($user, "Score", SORT_NUMERIC, SORT_DESC)
$user_asc = array_multisort($user, "Score", SORT_NUMERIC, SORT_ASC)
?>
Gewijzigd op 23/07/2010 09:08:55 door Robin van der Vliet
Je zou er zelf een functie voor kunnen schrijven die het sorteren voor je verzorgt?
En ja je hebt gelijk als je bedoeld dat je het een en ander eerst een stukkie uitelkaar moet trekken.
Je moet beter naar de gegeven voorbeeld kijken...
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?
//communicating with the parser, E_ALL: t-double-arrow-ja
$a[0]['Simple']['Username']='robin';
$a[0]['Simple']['ID']=1;
$a[0]['Simple']['Score'] = 3491;
$a[0]['Simple']['Died'] = 13;
$a[1]['Simple']['Username'] = 'name';
$a[1]['Simple']['ID'] = 418;
$a[1]['Simple']['Score'] = 131;
$a[1]['Simple']['Died'] = 914;
$a[2]['Simple']['Username'] = 'player';
$a[2]['Simple']['ID'] = 852;
$a[2]['Simple']['Score'] = 6174;
$a[2]['Simple']['Died'] = 12;
//expecting(-ja , na 'forech' dubbele data is verdacht: redundantie
//echo'<pre>';print_r($a);echo'</pre>';
$j11=0;$j21='Simple'; $j31='Username'; $j32='ID'; $j33='Score'; $j34='Died';
//function value_as_key($a){ we maken een result-table $r , global $r;
$i=0;
foreach($a as $k=>$v){
echo$k.$v[$j21][$j31].$v[$j21][$j33].'<br>';
$r[$i][0]=$a[$i]['Simple']['Score'];
$r[$i][1]=$k; $i++; }
echo'<pre>';print_r($r);echo'</pre>';
//return ;} //voor heel de array moet 1e sleutel.deel dynamisch: tellertje
//value_as_key($a);
//$user_desc=1; rsort:sorteert op waarde in omgekeerde v, kent nwe nrs als key toe
$rr=$r;rsort($rr);
//echo'<pre>';print_r($rr);echo'</pre>';
echo'simple aflopend desc<br>';
foreach($rr as $k=>$v){
echo $j31.'. => .'.$a[$v[1]][$j21][$j31] .'';
echo $j32.'. => .'.$a[$v[1]][$j21][$j32] .'';
echo $j33.'. => .'.$a[$v[1]][$j21][$j33] .'<br>';}
//$user_asc=1;
$rs=$r;sort($rs);
//echo'<pre>';print_r($rs);echo'</pre>';
echo'simple toenemend asc<br>';
foreach($rs as $k=>$v){
echo $j31.'. => .'.$a[$v[1]][$j21][$j31] .'';
echo $j32.'. => .'.$a[$v[1]][$j21][$j32] .'';
echo $j33.'. => .'.$a[$v[1]][$j21][$j33] .'<br>';}
?>
//communicating with the parser, E_ALL: t-double-arrow-ja
$a[0]['Simple']['Username']='robin';
$a[0]['Simple']['ID']=1;
$a[0]['Simple']['Score'] = 3491;
$a[0]['Simple']['Died'] = 13;
$a[1]['Simple']['Username'] = 'name';
$a[1]['Simple']['ID'] = 418;
$a[1]['Simple']['Score'] = 131;
$a[1]['Simple']['Died'] = 914;
$a[2]['Simple']['Username'] = 'player';
$a[2]['Simple']['ID'] = 852;
$a[2]['Simple']['Score'] = 6174;
$a[2]['Simple']['Died'] = 12;
//expecting(-ja , na 'forech' dubbele data is verdacht: redundantie
//echo'<pre>';print_r($a);echo'</pre>';
$j11=0;$j21='Simple'; $j31='Username'; $j32='ID'; $j33='Score'; $j34='Died';
//function value_as_key($a){ we maken een result-table $r , global $r;
$i=0;
foreach($a as $k=>$v){
echo$k.$v[$j21][$j31].$v[$j21][$j33].'<br>';
$r[$i][0]=$a[$i]['Simple']['Score'];
$r[$i][1]=$k; $i++; }
echo'<pre>';print_r($r);echo'</pre>';
//return ;} //voor heel de array moet 1e sleutel.deel dynamisch: tellertje
//value_as_key($a);
//$user_desc=1; rsort:sorteert op waarde in omgekeerde v, kent nwe nrs als key toe
$rr=$r;rsort($rr);
//echo'<pre>';print_r($rr);echo'</pre>';
echo'simple aflopend desc<br>';
foreach($rr as $k=>$v){
echo $j31.'. => .'.$a[$v[1]][$j21][$j31] .'';
echo $j32.'. => .'.$a[$v[1]][$j21][$j32] .'';
echo $j33.'. => .'.$a[$v[1]][$j21][$j33] .'<br>';}
//$user_asc=1;
$rs=$r;sort($rs);
//echo'<pre>';print_r($rs);echo'</pre>';
echo'simple toenemend asc<br>';
foreach($rs as $k=>$v){
echo $j31.'. => .'.$a[$v[1]][$j21][$j31] .'';
echo $j32.'. => .'.$a[$v[1]][$j21][$j32] .'';
echo $j33.'. => .'.$a[$v[1]][$j21][$j33] .'<br>';}
?>
Ook moet het wel met wat prutswerk mogelijk zijn om het te doen met multisort, maar zo'n manier als all de leest zou ik niet doen.
Karl Karl op 26/07/2010 10:26:14:
...dat betekend dus dat je dan al de data kan sorteren....
En hoe?
Dat ligt er maar net aan hoe je de data ophaalt.