PHP添加数组和循环代码

时间:2014-04-03 15:48:37

标签: php

我有一个工作正常的课程,但我需要做多个结果。

以下是当前代码:

$url = 'http://mydomain.com';
$keyword = 'somekeyword';

$RankChecker=new RankChecker(1,5);

$result=$RankChecker->find($url,$keyword);

if ($result!==false) {

    echo "Your website is found at page number  ".$result["page"].".";

}

让它读取多个网址/关键字的最佳方式是什么?

2 个答案:

答案 0 :(得分:1)

将URL和关键字放入数组并循环遍历:

$urls = array(
    'http://mydomain.com' => 'somekeyword',
    'http://myotherdomain.com' => 'someotherkeyword'
);

$RankChecker=new RankChecker(1,5);

foreach($urls as $url => $keyword) {
    $result=$RankChecker->find($url,$keyword);

    if ($result!==false) {

        echo "Website " . $url . " is found at page number  ".$result["page"].".";

    }
}

答案 1 :(得分:1)

使用数组:

<?php

$websites[1] = array('url' => 'http://mydomain.com', 'keyword' => 'somekeyword');
$websites[2] = array('url' => 'http://mydomain2.com', 'keyword' => 'somekeyword2');
$websites[3] = array('url' => 'http://mydomain3.com', 'keyword' => 'somekeyword3');
// etc...

foreach ($websites as $val) 
{
    $RankChecker=new RankChecker(1,5);

    $result=$RankChecker->find($val['url'], $val['keyword']);

    if ($result!==false) {

        echo "Your website is found at page number  ".$result["page"].".";

    }   
}