我有一个数组,其中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,因此我将该号码作为链接的标题标记。
答案 0 :(得分:1)
你在问什么,我不明白,你想用$ value替换PLACEHOLDER然后这样做
$names = array(
"<a href='http://skyler.com' title='".$PLACEHOLDER."'>Skyler</a>",
"<a href='http://jesse.com' title='".$PLACEHOLDER."'>Jesse</a>"
);
答案 1 :(得分:1)
使用str_replace
将PLACEHOLDER替换为$value
foreach ($count as $key => $value) {
$key = str_replace('PLACEHOLDER', $value, $key); //<--replace PLACEHOLDER here
echo $value . ' – ' . $key . '<br />';
}
答案 2 :(得分:1)
foreach ($count as $key => $value) {
echo $value . ' – ' . str_replace('PLACEHOLDER', $value, $key) . '<br />';
}
答案 3 :(得分:1)
正确的代码是:
foreach ($count as $key => $value) {
echo str_replace('PLACEHOLDER', $value, $key); //this will replace placeholder with number of tags
echo '<br />';
}