我有一个数组,其中 PLACEHOLDER 是我稍后在代码中得到的变量 $value 的占位符:
$names = array(
"<a href='http://skyler.com' title='PLACEHOLDER'>Skyler</a>",
"<a href='http://jesse.com' title='PLACEHOLDER'>Jesse</a>",
"<a href='http://walter.com' title='PLACEHOLDER'>Walter</a>",
"<a href='http://skyler.com' title='PLACEHOLDER'>Skyler</a>",
"<a href='http://hank.com' title='PLACEHOLDER'>Hank</a>",
"<a href='http://marie.com' title='PLACEHOLDER'>Marie</a>",
"<a href='http://walter.com' title='PLACEHOLDER'>Walter</a>",
"<a href='http://walter.com' title='PLACEHOLDER'>Walter</a>",
"<a href='http://jesse.com' title='PLACEHOLDER'>Jesse</a>",
);
为了检查数组中相等值的频率,我用array_count_values
.
$count = array_count_values($names);
foreach ($count as $key => $value) {
echo $value . ' – ' . $key . '<br />';
}
所以我得到这样的东西:
3 – <a href='http:/walter.com' title='PLACEHOLDER'>Walter</a>
2 – <a href='http://jesse.com' title='PLACEHOLDER'>Jesse</a>
2 – <a href='http://skyler.com' title='PLACEHOLDER'>Skyler</a>
1 – <a href='http://hank.com' title='PLACEHOLDER'>Hank</a>
1 – <a href='http://marie.com' title='PLACEHOLDER'>Marie</a>
现在我将 PLACEHOLDER 替换为 $value,因此我将数字作为链接的标题标签。