개체 속성에 따라 개체 배열을 정렬해야합니다. 기본적 으로이 픽스쳐 테이블을 보고 "Liverpool FC"와 "Manchester City FC"에 주목하면 점수가 같은 것을 볼 수 있으므로 다른 값이 더 높은 것에 따라 정렬됩니다.
이제 내 것을 살펴보면 포인트를 기준으로 주문하고 있으며 여러 값을 기준으로 주문하는 방법을 모르겠습니다.
데이터는 클래스의 인스턴스 내에 저장되며,이 인스턴스는 팀 이름 인 Array(
) 내에 저장됩니다 Key
.
아래는 데이터를 관리하는 클래스입니다. 첫 번째 링크의 순서대로 개체가 정렬되도록 배열을 다시 정렬하려면 어떻게해야합니까?
class Calc {
private $config;
public $win = 0, $draw = 0, $loss = 0, $goalFor = 0, $goalConc = 0;
public function __construct($payload = array(0,0)) {
// Load config file
$this->config = parse_ini_file('config.ini', true);
// Add wins, losses, draws, goals for and goal conceived
$this->addData($payload);
}
// Linked data updated, ammend values
public function calcPlays() {
return 0 + $this->win + $this->draw + $this->loss;
}
public function calcPoints() {
// Add 0 to ensure value is an int
return $this->win * ($this->config['winPoints']) + ($this->draw * $this->config['drawPoints']) + ($this->loss * $this->config['lossPoints']);
}
public function calcGoalDifference() {
return ($this->goalFor - $this->goalConc);
}
public function addData($data) {
// Append goal data
$this->goalFor += $data[0];
$this->goalConc += $data[1];
// Win, Loss or Draw
if ($data[0] > $data[1]) { $this->win++;} elseif
($data[0] < $data[1]) { $this->loss++;} elseif
($data[0] == $data[1]) { $this->draw++;}
}
}
편집하다:
내 데이터는 이제 어디에나 있습니다.
1 Burnley FC 13 4 2 7 12 21 -9 14
2 Leicester City FC 13 3 4 6 16 22 -6 13
3 Crystal Palace FC 13 3 2 8 21 26 -5 11
4 Swansea City FC 13 2 3 8 16 26 -10 9
5 Arsenal FC 13 8 4 1 28 13 15 28
나는 내 수표가 잘못된 길이라고 가정하고 있는데 , $a
이보다 크거나 같으면 $b
다음 수표로 이동하지 않으면 true를 반환 한다고 가정했습니다 .
코드:
// Sort teams by points
uasort($teamData, function($a, $b) {
if ($a->calcPoints() < $b->calcPoints() && $a->calcPoints() !== $b->calcPoints()) {
return true;
} elseif ($a->calcGoalDifference() < $b->calcGoalDifference() && $a->calcGoalDifference() !== $b->calcGoalDifference()) {
return true;
} elseif($a->goalConc < $b->goalConc) {
return true;
}
return false;
});
usort 를 사용 하고 다른 값을 비교하고 그에 따라 정렬하는 함수를 작성할 수 있습니다 .
라인을 따라 뭔가 :
uasort($teamData, function ($a, $b)
{
if ( $a->calcPoints() < $b->calcPoints() )
{
return 1;
}
elseif ( $a->calcPoints() <= $b->calcPoints() && $a->calcGoalDifference() < $b->calcGoalDifference() )
{
return 1;
}
elseif ( ($a->calcPoints() <= $b->calcPoints() && $a->calcGoalDifference() <= $b->calcGoalDifference()) && $a->goalConc < $b->goalConc )
{
return 1;
}
return 0;
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다