php heap,PHP SplHeap 用法 手册 | 示例代码

To have a good idea what you can do with SplHeap, I created a little example script that will show the rankings of Belgian soccer teams in the Jupiler League.

* A class that extends SplHeap for showing rankings in the Belgian

* soccer tournament JupilerLeague

*/classJupilerLeagueextendsSplHeap{/**

* We modify the abstract method compare so we can sort our

* rankings using the values of a given array

*/public functioncompare($array1,$array2)

{$values1=array_values($array1);$values2=array_values($array2);

if ($values1[0] ===$values2[0]) return0;

return$values1[0]

}

}// Let's populate our heap here (data of 2009)$heap= newJupilerLeague();$heap->insert(array ('AA Gent'=>15));$heap->insert(array ('Anderlecht'=>20));$heap->insert(array ('Cercle Brugge'=>11));$heap->insert(array ('Charleroi'=>12));$heap->insert(array ('Club Brugge'=>21));$heap->insert(array ('G. Beerschot'=>15));$heap->insert(array ('Kortrijk'=>10));$heap->insert(array ('KV Mechelen'=>18));$heap->insert(array ('Lokeren'=>10));$heap->insert(array ('Moeskroen'=>7));$heap->insert(array ('Racing Genk'=>11));$heap->insert(array ('Roeselare'=>6));$heap->insert(array ('Standard'=>20));$heap->insert(array ('STVV'=>17));$heap->insert(array ('Westerlo'=>10));$heap->insert(array ('Zulte Waregem'=>15));// For displaying the ranking we move up to the first node$heap->top();// Then we iterate through each node for displaying the resultwhile ($heap->valid()) {

list ($team,$score) =each($heap->current());

echo$team.': '.$score.PHP_EOL;$heap->next();

}?>

This results in the following output:

Club Brugge: 21

Anderlecht: 20

Standard: 20

KV Mechelen: 18

STVV: 17

Zulte Waregem: 15

AA Gent: 15

G. Beerschot: 15

Charleroi: 12

Racing Genk: 11

Cercle Brugge: 11

Kortrijk: 10

Lokeren: 10

Westerlo: 10

Moeskroen: 7

Roeselare: 6

Hope this example paved the way for more complex implementations of SplHeap.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值