Pagina 1 din 1

Aranjare array din json in php

Scris: Mar Aug 18, 2015
de MAR
Salutari, vin si eu cu o intrebare la Dvs.
Am urmatorul script :

Cod: Selectaţi tot

if(($json = @file_get_contents("http://api.ets2mp.com/servers/")) !== FALSE) {//Schaut ob die Datei online/Verfügbar ist.
$json = json_decode($json, TRUE);//String wird Dekodiert
if(json_last_error() == JSON_ERROR_NONE) {//Schaut ob Fehler drine sind, die nicht gelesen werden können.
foreach($json['response'] as $server) {//Eine kleine schrleife
if($server['online'] == 1) {//Server Status abfrage, Online.
echo ''.$server['name'].': Server Online, Spieler Online: '.$server['players'].'
'; //Text ausgabe
}else if($server['online'] == 0) {//Server Status abfrage, Offline.
echo $server['name'].': Server Offline
';//Text ausgabe 
}
} 
}
} 
Care preia informatii de pe link-ul

Cod: Selectaţi tot

http://api.ets2mp.com/servers/
Si le preia exact cum sunt afisate aici:

Cod: Selectaţi tot

{"error":"false","response":[{"id":1,"ip":"37.187.170.151","port":42860,"name":"Europe #1","shortname":"EU #1","online":true,"players":2084,"maxplayers":3500,"speedlimiter":1},{"id":3,"ip":"191.101.3.39","port":42860,"name":"United states #1","shortname":"US #1","online":true,"players":25,"maxplayers":500,"speedlimiter":1},{"id":4,"ip":"151.80.47.35","port":42880,"name":"Europe #2","shortname":"EU #2","online":true,"players":569,"maxplayers":2300,"speedlimiter":0},{"id":6,"ip":"1.asia.game.kat.pw","port":42860,"name":"Asia #1","shortname":"AS #1","online":true,"players":33,"maxplayers":500,"speedlimiter":1},{"id":7,"ip":"1.brazil.game.kat.pw","port":42860,"name":"South America #1","shortname":"SA #1","online":true,"players":18,"maxplayers":750,"speedlimiter":1},{"id":9,"ip":"139.162.29.242","port":42860,"name":"Asia #1 (Relay)","shortname":"AS[R] #1","online":true,"players":33,"maxplayers":500,"speedlimiter":1}]}
Cum pot sa aranjez rezultatul afisat cum vreau eu, in ce ordine vreau eu? O ordine dupa (ID) ..sau cumva.

Aranjare array din json in php

Scris: Mar Aug 18, 2015
de MarPlo
Salut
Acel json e un array multidimensional; pentru a sorta astfel de array-uri in php poti sa folosesti functia de la aceasta pagina: https://coursesweb.net/php-mysql/sort-en ... l-array_cs

Uite un exemplu cu datele json din codul tau:

Cod: Selectaţi tot

function sortMultiArray($arr, $k, $sort) {
// Sorts an entire multi-dimensional array by one element of it
  $tmp = Array();
  foreach($arr as &$ma)  $tmp[] = &$ma[$k];
  $tmp = array_map('strtolower', $tmp);      // to sort case-insensitive
  array_multisort($tmp, $sort, $arr);
  return $arr;
}

$json ='{"error":"false","response":[{"id":1,"ip":"37.187.170.151","port":42860,"name":"Europe #1","shortname":"EU #1","online":true,"players":2084,"maxplayers":3500,"speedlimiter":1},{"id":3,"ip":"191.101.3.39","port":42860,"name":"United states #1","shortname":"US #1","online":true,"players":25,"maxplayers":500,"speedlimiter":1},{"id":4,"ip":"151.80.47.35","port":42880,"name":"Europe #2","shortname":"EU #2","online":true,"players":569,"maxplayers":2300,"speedlimiter":0},{"id":6,"ip":"1.asia.game.kat.pw","port":42860,"name":"Asia #1","shortname":"AS #1","online":true,"players":33,"maxplayers":500,"speedlimiter":1},{"id":7,"ip":"1.brazil.game.kat.pw","port":42860,"name":"South America #1","shortname":"SA #1","online":true,"players":18,"maxplayers":750,"speedlimiter":1},{"id":9,"ip":"139.162.29.242","port":42860,"name":"Asia #1 (Relay)","shortname":"AS[R] #1","online":true,"players":33,"maxplayers":500,"speedlimiter":1}]}';
$json = json_decode($json, true);

// sort and keeps the array from $json['response'] by "name" ascendingly
$json = sortMultiArray($json['response'], 'name', SORT_ASC);

// test
echo '<pre>';
var_export($json);  
- Array-ul fiind sortat cum vrei, poti folosi foreach() ca sa parcurgi datele si sa le afisezi in ordinea obtinuta.