2

我有一个查看论坛帖子脚本

if(isset($replys)){
    foreach ($replys as $reply) {
        echo $reply['name'].' | '.$reply['date'];
    }
}

输出如:

abc | 2013-1-1
123 | 2013-1-2
456 | 2013-1-3

然后我想随机展示广告,例如:

abc | 2013-1-1
Google Ads | testing
123 | 2013-1-2
456 | 2013-1-3

使用一些 if 语句:

if( is_string(substr(sha1(uniqid()),-1)) ){
// show ads
}

我怎样才能做到这一点?

4

1 回答 1

1

找一个随机位置插入广告:

if (is_string(substr(sha1(uniqid()),-1))) {
    // show ads
    $offset = mt_rand(0, count($replys) - 1);
    $ad = array('name' => 'Google Ads', 'date' => 'testing');

    $replys = array_splice($replys, $offset, 0, $ad);
}
于 2013-02-07T07:22:03.880 回答