J'ai quelque chose comme ceci:Tableau de tri d'objets PHP selon certains critères?
$i = 0;
foreach($tracks['results'] as $track){
$trackName[$i] = $track['name'];
$trackPlaycount[$track['name']] = $track['playcount'];
$trackPercent[$track['name']] = $track['percent'];
$i++;
}
$this->trackName = $trackName;
$this->trackPlaycount = $trackPlaycount;
$this->trackPercent = $trackPercent;
comment pourrais-je trier ces objets par Nombre de lectures? D'après ce que j'ai lu jusqu'à présent, je comprends que je devrais probablement créer une fonction de comparaison et ensuite le faire fonctionner avec usort(), n'est-ce pas? Mais je ne suis pas tout à fait sûr de savoir comment réaliser que ...
merci
modifier: ok, maintenant je l'ai comme ceci:
public function firstmethod(){
// there are some more parameters here of course
// but they worked before, not important for the problem
$i = 0;
foreach($tracks['results'] as $track){
$trackName[$i] = $track['name'];
$trackPlaycount[$track['name']] = $track['playcount'];
$trackPercent[$track['name']] = $track['percent'];
// this part is new
$tracksArray[$i] = array(
'name' => $trackName[$i],
'playcount' => $trackPlaycount[$track['name']],
'percentage' => $trackPercent[$track['name']]
);
$i++;
}
usort($tracksArray, array($this, 'sortByCount'));
$i = 0;
// this is to put the new sorted array into the old variables?
foreach($tracksArray as $temp){
$trackName[$i] = $temp['name'];
$trackPlaycount[$trackName[$i]] = $temp['playcount'];
$trackPercent[$trackName[$i]] = $temp['percentage'];
$i++;
}
$this->trackName = $trackName;
$this->trackPlaycount = $trackPlaycount;
$this->trackPercent = $trackPercent;
}
public function sortByCount($a, $b){
if($a["playcount"] == $b["playcount"]) {
return 0;
}
return ($a["playcount"] < $b["playcount"]) ? -1 : 1;
}
cela fonctionne maintenant ... merci tout le monde
Modifié ma réponse donc il inclut le tri par champs entiers si cela vous donnait le problème, j'espère que ça aide – edorian