0

我有一个数组,其中 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,因此我将数字作为链接的标题标签。

4

4 回答 4

1

你在问什么,我不明白,你想用 $value 替换 PLACEHOLD 然后这样做

$names = array(
"<a href='http://skyler.com' title='".$PLACEHOLDER."'>Skyler</a>",
"<a href='http://jesse.com' title='".$PLACEHOLDER."'>Jesse</a>"
);
于 2012-11-14T11:04:21.520 回答
1

用于str_replace将 PLACEHOLDER 替换为$value

foreach ($count as $key => $value) {
    $key = str_replace('PLACEHOLDER', $value, $key); //<--replace PLACEHOLDER here
    echo $value . ' – ' . $key . '<br />';
}
于 2012-11-14T11:07:07.723 回答
1
foreach ($count as $key => $value) {
    echo $value . ' – ' . str_replace('PLACEHOLDER', $value, $key) . '<br />';
}
于 2012-11-14T11:10:19.080 回答
1

正确的代码是:

foreach ($count as $key => $value) {
    echo str_replace('PLACEHOLDER', $value, $key); //this will replace placeholder with number of tags
    echo '<br />';
}
于 2012-11-14T11:11:31.957 回答