Sortowanie tablic w php z danymi z XML

0

Witam!
Męczę się od dawna z problemem sortowania danych w tablicach.. może przejdę od razu do rzeczy.
Mam następujący plik xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE rF [
<!ENTITY rFEnt "rFactor Entity">
]>
<rFactorXML version="1.0">
<RaceResults>
<Setting>Multiplayer</Setting>
<ServerName>Need for Speed #3</ServerName>
<PlayerFile>Gilles</PlayerFile>
<DateTime>1372094513</DateTime>
<TimeString>2013/06/24 19:21:53</TimeString>
<Mod>ToyotaSupraMKIV.rfm</Mod>
<Season></Season>
<TrackVenue>Iceland</TrackVenue>
<TrackCourse>Maikita Roads RFE Ready</TrackCourse>
<TrackEvent>Maikita Roads RFE Ready</TrackEvent>
<TrackLength>18051.8</TrackLength>
<GameVersion>1.255</GameVersion>
<Dedicated>1</Dedicated>
<ConnectionType upload="1000" download="3000">Custom</ConnectionType>
<MOTD></MOTD>
<RaceLaps>6</RaceLaps>
<RaceTime>60</RaceTime>
<MechFailRate>1</MechFailRate>
<DamageMult>50</DamageMult>
<FuelMult>1</FuelMult>
<TireMult>1</TireMult>
<VehiclesAllowed>|SUPRA</VehiclesAllowed>
<ParcFerme>3</ParcFerme>
<FixedSetups>0</FixedSetups>
<FreeSettings>11</FreeSettings>
<FixedUpgrades>0</FixedUpgrades>
<Race>
<DateTime>1372099093</DateTime>
<TimeString>2013/06/24 20:38:13</TimeString>
<Laps>6</Laps>
<Minutes>60</Minutes>
<Stream>
<Chat et="37.0">KlakieR: jedz tera wolniej jak my</Chat>
<Incident et="122.1">Gilles(3) reported contact (1.00) with Track</Incident>
<Incident et="199.4">Gilles(3) reported contact (1.00) with Track</Incident>
<Incident et="199.5">Gilles(3) reported contact (0.16) with Track</Incident>
<Incident et="199.9">Gilles(3) reported contact (0.03) with Track</Incident>
<Incident et="200.2">Gilles(3) reported contact (0.03) with Track</Incident>
<Score et="246.8">KlakieR(5) lap=0 point=1 t=205.722 et=246.750</Score>
<Score et="252.0">Gilles(3) lap=0 point=1 t=210.972 et=251.999</Score>
<Chat et="277.2">KlakieR: ..</Chat>
<Chat et="287.1">Gilles: ?</Chat>
<Score et="304.5">Bubble(1) lap=0 point=1 t=263.468 et=304.496</Score>
<Incident et="348.2">Gilles(3) reported contact (0.03) with Sign</Incident>
<Incident et="351.5">Gilles(3) reported contact (1.00) with Track</Incident>
<Score et="414.0">Gilles(3) lap=0 point=2 t=372.880 et=413.908</Score>
<Score et="418.0">KlakieR(5) lap=0 point=2 t=376.906 et=417.934</Score>
<Incident et="419.3">Gilles(3) reported contact (0.39) with Track</Incident>
<Incident et="419.4">Gilles(3) reported contact (0.84) with Track</Incident>
<Incident et="419.4">Gilles(3) reported contact (0.03) with Track</Incident>
<Incident et="420.0">Gilles(3) reported contact (0.48) with Track</Incident>
<Incident et="420.7">Gilles(3) reported contact (0.84) with Track</Incident>
<Incident et="420.8">Gilles(3) reported contact (0.74) with Track</Incident>
<Incident et="420.8">Gilles(3) reported contact (0.65) with Track</Incident>
<Incident et="420.9">Gilles(3) reported contact (0.71) with Track</Incident>
<Incident et="483.8">KlakieR(5) reported contact (0.10) with Track</Incident>
<Score et="518.6">Bubble(1) lap=0 point=2 t=477.533 et=518.560</Score>
<Score et="567.6">Gilles(3) lap=1 point=0 t=526.497 et=567.525</Score>
<Score et="574.3">KlakieR(5) lap=1 point=0 t=533.222 et=574.250</Score>
<Incident et="716.4">Gilles(3) reported contact (0.39) with Cone</Incident>
<Incident et="716.4">Gilles(3) reported contact (0.13) with Track</Incident>
<Incident et="716.4">Gilles(3) reported contact (0.16) with Cone</Incident>
<Incident et="716.4">Gilles(3) reported contact (0.23) with Cone</Incident>
<Incident et="717.1">Gilles(3) reported contact (1.00) with Track</Incident>
<Incident et="717.1">Gilles(3) reported contact (0.39) with Track</Incident>
<Incident et="717.1">Gilles(3) reported contact (1.00) with Track</Incident>
<Incident et="717.1">Gilles(3) reported contact (0.68) with Track</Incident>
<Incident et="717.2">Gilles(3) reported contact (0.58) with Track</Incident>
<Incident et="717.2">Gilles(3) reported contact (0.06) with Track</Incident>
<Score et="763.3">Gilles(3) lap=1 point=1 t=195.706 et=763.231</Score>
<Score et="766.4">KlakieR(5) lap=1 point=1 t=192.089 et=766.339</Score>
<Chat et="891.8">KlakieR: tez</Chat>
<Score et="908.5">KlakieR(5) lap=1 point=2 t=334.248 et=908.498</Score>
<Chat et="917.3">KlakieR: ja 12;]</Chat>
<Chat et="938.6">KlakieR: to uznajmy 1 wyscig za ukocnzony;p</Chat>
<Score et="942.0">Gilles(3) lap=1 point=2 t=374.469 et=941.994</Score>
<Chat et="1011.7">KlakieR: to popcham;]</Chat>
<Incident et="1093.5">Gilles(3) reported contact (0.29) with Sign</Incident>
<Incident et="1093.8">Gilles(3) reported contact (0.06) with Sign</Incident>
<Chat et="1114.4">KlakieR: styklo by mu;</Chat>
<Chat et="1159.2">KlakieR: naprawiasz sie?;]</Chat>
<Score et="1196.8">Gilles(3) lap=2 point=0 t=629.256 et=1196.781</Score>
<Score et="1198.1">KlakieR(5) lap=2 point=0 t=623.775 et=1198.025</Score>
<Chat et="1212.1">KlakieR: ja sie che posciagac a eni</Chat>
<Incident et="1278.8">KlakieR(5) reported contact (0.23) with Cone</Incident>
<Incident et="1279.0">KlakieR(5) reported contact (0.19) with Cone</Incident>
<Incident et="1279.2">KlakieR(5) reported contact (0.19) with Cone</Incident>
<Incident et="1279.4">KlakieR(5) reported contact (0.16) with Cone</Incident>
<Incident et="1381.6">KlakieR(5) reported contact (0.10) with another vehicle Gilles(3)</Incident>
<Incident et="1381.7">Gilles(3) reported contact (0.03) with another vehicle KlakieR(5)</Incident>
<Incident et="1386.7">KlakieR(5) reported contact (0.03) with Track</Incident>
<Score et="1404.8">Gilles(3) lap=2 point=1 t=208.040 et=1404.821</Score>
<Score et="1405.2">KlakieR(5) lap=2 point=1 t=207.094 et=1405.119</Score>
<Incident et="1460.7">Gilles(3) reported contact (0.19) with another vehicle KlakieR(5)</Incident>
<Incident et="1515.0">Gilles(3) reported contact (1.00) with Sign</Incident>
<Score et="1547.2">KlakieR(5) lap=2 point=2 t=349.089 et=1547.114</Score>
<Score et="1550.5">Gilles(3) lap=2 point=2 t=353.644 et=1550.425</Score>
<Incident et="1591.0">Gilles(3) reported contact (1.00) with Track</Incident>
<Incident et="1591.2">Gilles(3) reported contact (0.06) with Track</Incident>
<Incident et="1667.1">Gilles(3) reported contact (0.10) with Sign</Incident>
<Score et="1693.3">KlakieR(5) lap=3 point=0 t=494.975 et=1693.000</Score>
<Score et="1697.8">Gilles(3) lap=3 point=0 t=500.970 et=1697.751</Score>
<Score et="1880.9">KlakieR(5) lap=3 point=1 t=187.879 et=1880.879</Score>
<Score et="1883.9">Gilles(3) lap=3 point=1 t=186.092 et=1883.843</Score>
<Incident et="1984.2">KlakieR(5) reported contact (0.55) with Track</Incident>
<Incident et="1984.8">KlakieR(5) reported contact (0.42) with Cone</Incident>
<Incident et="1984.9">KlakieR(5) reported contact (0.45) with Cone</Incident>
<Score et="2020.7">KlakieR(5) lap=3 point=2 t=327.660 et=2020.660</Score>
<Score et="2024.3">Gilles(3) lap=3 point=2 t=326.458 et=2024.209</Score>
<Score et="2169.3">Gilles(3) lap=4 point=0 t=471.481 et=2169.232</Score>
<Score et="2169.3">KlakieR(5) lap=4 point=0 t=476.262 et=2169.262</Score>
<Incident et="2216.2">Gilles(3) reported contact (0.03) with another vehicle KlakieR(5)</Incident>
<Incident et="2303.8">Gilles(3) reported contact (0.16) with Track</Incident>
<Incident et="2304.2">Gilles(3) reported contact (1.00) with Track</Incident>
<Incident et="2304.2">Gilles(3) reported contact (1.00) with Track</Incident>
<Incident et="2304.3">Gilles(3) reported contact (1.00) with Track</Incident>
<Incident et="2306.5">KlakieR(5) reported contact (0.65) with another vehicle Gilles(3)</Incident>
<Incident et="2306.5">KlakieR(5) reported contact (0.10) with another vehicle Gilles(3)</Incident>
<Incident et="2306.5">KlakieR(5) reported contact (0.35) with another vehicle Gilles(3)</Incident>
<Incident et="2306.5">Gilles(3) reported contact (1.00) with another vehicle KlakieR(5)</Incident>
<Incident et="2306.5">KlakieR(5) reported contact (0.74) with another vehicle Gilles(3)</Incident>
<Incident et="2306.5">KlakieR(5) reported contact (0.23) with another vehicle Gilles(3)</Incident>
<Incident et="2306.5">KlakieR(5) reported contact (0.16) with another vehicle Gilles(3)</Incident>
<Incident et="2306.5">Gilles(3) reported contact (0.58) with another vehicle KlakieR(5)</Incident>
<Incident et="2307.1">Gilles(3) reported contact (0.26) with Track</Incident>
<Incident et="2307.1">Gilles(3) reported contact (0.29) with Track</Incident>
<Incident et="2307.3">Gilles(3) reported contact (0.10) with Track</Incident>
<Incident et="2307.5">Gilles(3) reported contact (0.03) with Track</Incident>
<Incident et="2307.6">Gilles(3) reported contact (0.42) with Track</Incident>
<Incident et="2336.5">Gilles(3) reported contact (1.00) with Track</Incident>
<Chat et="2336.8">Gilles: fr tyre puncture</Chat>
<Incident et="2336.8">Gilles(3) reported contact (1.00) with Track</Incident>
<Incident et="2336.9">Gilles(3) reported contact (0.10) with Wing</Incident>
<Incident et="2336.9">Gilles(3) reported contact (0.26) with Track</Incident>
<Incident et="2337.0">Gilles(3) reported contact (0.23) with Track</Incident>
<Incident et="2337.1">Gilles(3) reported contact (0.06) with Track</Incident>
<Chat et="2345.9">Gilles: i przod juz tys</Chat>
<Chat et="2356.1">KlakieR: to kuniec?</Chat>
<Chat et="2379.7">Gilles: no</Chat>
<Chat et="2388.5">Gilles: nie wyjechalbym  bo mialem 11 fps</Chat>
<Chat et="2397.3">Gilles: se przydzeil pkt;]</Chat>
<Chat et="2400.9">Gilles: i mi napisz</Chat>
<Sent et="2402.0">Gilles has left the game but vehicle has been stored in the garage</Sent>
<Sent et="2407.7">Bubble has left the game but vehicle has been stored in the garage</Sent>
<Sent et="2416.9">KlakieR has left the game but vehicle has been stored in the garage</Sent>
</Stream>
<MostLapsCompleted>4</MostLapsCompleted>
<Driver>
<Name>Gilles</Name>
<Connected>1</Connected>
<VehFile>GZ-YELLOW.VEH</VehFile>
<UpgradeCode>8d130000 00000000</UpgradeCode>
<VehName>Yellow</VehName>
<CarType>Supra MKIV</CarType>
<CarClass>Supra MKIV</CarClass>
<CarNumber>6</CarNumber>
<TeamName>&apos;93 Supra GZ</TeamName>
<isPlayer>0</isPlayer>
<GridPos>1</GridPos>
<Position>2</Position>
<ClassGridPos>1</ClassGridPos>
<ClassPosition>2</ClassPosition>
<Points>5</Points>
<ClassPoints>5</ClassPoints>
<LapRankIncludingDiscos>1</LapRankIncludingDiscos>
<Lap num="1" p="1" et="41.0279" s1="210.9715" s2="161.9081" s3="153.6173" fuel="0.067">526.4969</Lap>
<Lap num="2" p="2" et="567.5249" s1="195.7057" s2="178.7633" s3="254.7867" fuel="0.027" pit="1">629.2557</Lap>
<Lap num="3" p="2" et="1196.7805" s1="208.0404" s2="145.6038" s3="147.3262" fuel="0.925">500.9704</Lap>
<Lap num="4" p="1" et="1697.7509" s1="186.0922" s2="140.3657" s3="145.0234" fuel="0.855">471.4813</Lap>
<BestLapTime>471.4813</BestLapTime>
<Laps>4</Laps>
<Pitstops>1</Pitstops>
<FinishStatus>DNF</FinishStatus>
<DNFReason>Suspension</DNFReason>
<ControlAndAids startLap="1" endLap="4">PlayerControl,ABS=2,Clutch,AutoLift,AutoBlip</ControlAndAids>
</Driver>
<Driver>
<Name>KlakieR</Name>
<Connected>1</Connected>
<VehFile>GZ-WHITE.VEH</VehFile>
<UpgradeCode>cd050000 00000000</UpgradeCode>
<VehName>White</VehName>
<CarType>Supra MKIV</CarType>
<CarClass>Supra MKIV</CarClass>
<CarNumber>2</CarNumber>
<TeamName>&apos;93 Supra GZ</TeamName>
<isPlayer>0</isPlayer>
<GridPos>2</GridPos>
<Position>1</Position>
<ClassGridPos>2</ClassGridPos>
<ClassPosition>1</ClassPosition>
<Points>8</Points>
<ClassPoints>8</ClassPoints>
<LapRankIncludingDiscos>2</LapRankIncludingDiscos>
<Lap num="1" p="2" et="41.0279" s1="205.7220" s2="171.1841" s3="156.3159" fuel="0.220">533.2220</Lap>
<Lap num="2" p="2" et="574.2500" s1="192.0887" s2="142.1598" s3="289.5266" fuel="0.176" pit="1">623.7750</Lap>
<Lap num="3" p="1" et="1198.0250" s1="207.0942" s2="141.9947" s3="145.8861" fuel="0.431">494.9750</Lap>
<Lap num="4" p="2" et="1693.0000" s1="187.8787" s2="139.7815" s3="148.6018" fuel="0.369">476.2620</Lap>
<BestLapTime>476.2620</BestLapTime>
<Laps>4</Laps>
<Pitstops>1</Pitstops>
<FinishStatus>DNF</FinishStatus>
<DNFReason>DNF</DNFReason>
<ControlAndAids startLap="1" endLap="4">PlayerControl,ABS=2,Clutch,AutoBlip</ControlAndAids>
</Driver>
<Driver>
<Name>Bubble</Name>
<Connected>1</Connected>
<VehFile>GZ-BEIGE.VEH</VehFile>
<UpgradeCode>2c140000 00000000</UpgradeCode>
<VehName>Beige</VehName>
<CarType>Supra MKIV</CarType>
<CarClass>Supra MKIV</CarClass>
<CarNumber>3</CarNumber>
<TeamName>&apos;93 Supra GZ</TeamName>
<isPlayer>0</isPlayer>
<GridPos>3</GridPos>
<Position>3</Position>
<ClassGridPos>3</ClassGridPos>
<ClassPosition>3</ClassPosition>
<Points>0</Points>
<ClassPoints>0</ClassPoints>
<LapRankIncludingDiscos>3</LapRankIncludingDiscos>
<Laps>0</Laps>
<Pitstops>0</Pitstops>
<FinishStatus>DNF</FinishStatus>
<DNFReason>Engine</DNFReason>
</Driver>
</Race>
</RaceResults>
</rFactorXML>

I jakiś czas temu udało mi się uciułać taki oto skrypt php - wkleje fragment, który przysparza problemy, z góry przepraszam za nie profesjonalne komentarze:

 
<?php
foreach($wyniki -> RaceResults -> Race -> Driver as $kierowca) // pętla wyci±gaj±ca dane
{
$tablica["poz"] = $kierowca -> Position;     // przypisanie do tablic
$tablica["name"] = $kierowca -> Name;        // danych z pliku xml
$tablica["grid"] = $kierowca -> GridPos;     // trzeba pamietac ze na jedn± tablice
$tablica["best"] = $kierowca -> BestLapTime; // przypada kilka warto¶ci. w tym przypadku 3
$tablica["laps"] = $kierowca -> Laps;        // bo tyle osób wystartowało w wy¶cigu

array_multisort
(
$tablica["poz"], 
$tablica["name"],
$tablica["grid"],
$tablica["best"],
$tablica["laps"]);
  // instrukcja, która niby ma sortować wielo kolumnowo ale ni ch.. nie działa

// to poniżej to jest wypisanie tabelki z danymi
// jeszcze tak nadmienie że najlepszy czas okr±żenie podawany jest w sekundach... 
echo 
'<table cellspacing="0" summary="" border="2">
   <tr>
     <td width="20">'.$tablica["poz"].'</td><td width="50">'.$tablica["name"].'</td><td width="20">'.$tablica["grid"].'</td><td width="70">'.$tablica["best"].'</td><td width="20">'.$tablica["laps"].'</td>
   </tr>
 </table>';
}
?>

a to jest efekt tego skryptu(tabelka): http://pr.c0.pl/test/wyniki.php

Cały mój problem polega na tym, że nie wiem jak posortować te dane wg pierwszej kolumny. Mógłby ktoś to troszkę ogarnąć? bo jestem bardzo początkujący i brak mi pomysłów...
Jeśli ten kod jest do niczego to nie krzyczcie bardzo ;)

Z góry dzięki za pomoc!
Pozdrawiam Gilles!

0

Dzieki koledzy za pomoc ;]
Problem dalej istnieje, ale projekt zdążył upaść, wiec się już nie wysilajcie.

0

Myśleliśmy że przez 9 dni domyślisz się kliknąć w

 array_multisort 

w swoim poście i znaleźć tam odpowiedni przykład, przepraszamy że zakładaliśmy że masz odrobinę inteligencji.

0

dobra.. dzieki.. serio myślisz że tego nie widziałem? gdyby mi to pomogło to bym nie zakładał tu tematu... ale już nie ważne...

0

Jeżeli widziałeś to jeszcze gorzej o tobie świadczy, bo tam prawie na żywca twój przykład jest.

0

dobra, to sobie daje spokój z programowaniem. cześć dzięki. zajme się rolnictwem

1

user image

a tak serio to nawet tam nie masz tablicy
wpisujesz dane wprawdzie do tablicy, ale w stylu:

$tab['a'] = $a;
$tab['b'] = $b;

a potem robisz:

array_multisort($tab['a'], $tab['b']);

równie dobrze mógłbyś napisać:

array_multisort($a, $b);

i będzie to tak samo bez sensu ale mniej pisania

NAJPIERW uzupełnij tablicę (podpowiedź: teraz nie masz tablicy danych, masz vector przechowujący pojedynczą krotkę), POTEM ją posortuj a NA KOŃCU wyświetl

teraz pobierasz po elemencie, sortujesz ten jeden element (wtf) i go od razu wyświetlasz (jak można wyświetlać elementy przed posortowaniem i liczyć na to że będą w dobrej kolejności?)

0

no i chociaż tyle.. dzieki, teraz postaram się coś pokombinować..
jestem nowy w temacie PHP i nie kryje tego, w moich "skryptach" mogą się pojawiać różne kwiatki.. ale po to założyłem temat na profesjonalnym forum w dziale Newbie, by ktoś wskazał mi poprawną droge a nie informował że jestem "mało inteligentny"

0

Przecież masz tam doskonały przykład:
http://us3.php.net/manual/en/function.array-multisort.php#57161

1 użytkowników online, w tym zalogowanych: 0, gości: 1