问:我如何结合这两个foreach循环

时间:2015-05-29 16:26:29

标签: php json loops foreach simple-html-dom

我有一个简单的html dom查询,它从足球灯具源读取信息,我也加载了一个json源。

这是我的完整代码:

<?php
    header('Content-Type: text/html; charset=utf-8');
    ini_set("user_agent", "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.60 Safari/537.17");

include_once('simple_html_dom.php');

ini_set('display_errors', true);
error_reporting(E_ALL);

$str = file_get_contents('general.json');

$json = json_decode($str,true);

$filename = "source.html";
$html = file_get_html($filename);

class matches {
    var $day;
    var $kickofftime;
    var $result;

    function matches ($day, $kickofftime, $tip){
       $this->day=$day;
       $this->kickofftime=$kickofftime;
       $this->result=$result;
       return $this;
    }
}


$i=0;

$day=$html->find('h1',0);
$day->plaintext;
$day=str_replace("<h1>TODAY FOOTBALL FIXTURES: ","", $day);
$day=str_replace("</h1>","", $day);
$matchday = str_replace(array('MONDAY ', 'TUESDAY ', 'WEDNESDAY ', 'THURSDAY ', 'FRIDAY ',  'SATURDAY ',  'SUNDAY '), '', $day);
$matchday=str_replace(" ","-", $matchday);
$matchday=date('Y-m-d', strtotime($matchday));

foreach($html->find('table.fixtures') as $matches) 
{

    foreach ($matches->find('tr[class=a1],tr[class=a2]') as $matchesTR) {

        $kickofftime=$matchesTR->find('td[class=a11],td[class=a21]',0)->plaintext;
        $kodate = date('Y-m-d H:i:s', strtotime("$matchday $kickofftime +1 hour"));
        $result=$matchesTR->find('td');

        echo $kodate;
        echo $result[6]->plaintext.'<br>' ; 

        $i++;


    }
}

//Here is the 2nd foreach with the data of JSON source:

foreach($json as $key => $value) {

    $value = json_decode($value, true);
    echo $value["country"] . ", " . $value["competition"] . "  " . $value["club"] . "<br>";
}

// clean up memory
$html->clear();
unset($html);

?>

当前来自简单的html dom html源代码:

2014-12-23 20:00:00 2-1
2014-12-23 11:00:00 3-1
2014-12-26 08:00:00 1-1

JSON来源的结果:

美国Copa America Boca Juniors
欧洲德甲汉诺威
亚洲JLeague名古屋

我想将这两个结果合并到一个foreach中,我希望得到这个结果:

2014-12-23 20:00:00 2-1美国Copa America Boca Juniors
2014-12-23 11:00:00 3-1欧洲德甲汉诺威
2014-12-26 08:00:00 1-1亚洲JLeague名古屋

我希望有一些专家可以帮助我,因为我尝试了很多变化但没有结果。我从专家那里得到了一些建议(带代码),但每次都有错误。使用我的代码没有错误,但我需要其他解决方案,因为我想将所有变量放在一个foreach中。非常感谢,我希望有人可以帮助我使用代码,因为我不是在PHP的高级别。再次感谢!

我想把这两个foreach放到一个foreach中,但我不想创建一个新的数组,因为我不需要。

2 个答案:

答案 0 :(得分:0)

假设每个项目中的项目总数相同,并且它们匹配1到1,2到2,您可以这样做:

$htmlDates = array();
$jsonLeag = array();

foreach($html->find('table.fixtures') as $matches) {
    foreach($matches->find('tr[class=a1],tr[class=a2]') as $matchesTR) {
        $kickofftime=$matchesTR->find('td[class=a11],td[class=a21]',0)->plaintext;
        $kodate = date('Y-m-d H:i:s', strtotime("$matchday $kickofftime +1 hour"));
        $result=$matchesTR->find('td');

        //echo $kodate;
        //echo $result[6]->plaintext.'<br>' ;
        $htmlDates[] = $kodates;
    }
}

//Here is the 2nd foreach with the data of JSON source:

foreach($json as $key => $value) {
    $value = json_decode($value, true);
    //echo $value["country"] . ", " . $value["competition"] . "  " . $value["club"] . "<br>";
    $jsonLeag[] = $value["country"] . ", " . $value["competition"] . "  " . $value["club"];
}

if(count($htmlDates) < count($jsonLeag)){
    for($i=0;$i<count($htmlData);$i++){
        echo $htmlData[$i] . " " . $jsonLeag[$i] . "<br />\r\n";
    }
} else {
    for($i=0;$i<count($jsonLeag);$i++){
        echo $htmlData[$i] . " " . $jsonLeag[$i] . "<br />\r\n";
    }
}

由于您首先拥有嵌套列表并且没有将两个数据集绑定在一起,因此没有简单的方法来运行一个循环并从两者中获取数据。将所需数据推送到每个集合的数组中更容易,然后使用一个计数器遍历两个数组。这里的警告是,如果有一个额外的元素,你将得到缺失的结果或错误。

答案 1 :(得分:0)

保存json数组的键并在每个循环中使用下一个。

$json_keys = array_keys($json);
$i_key = 0;
foreach($html->find('table.fixtures') as $matches) 
{

    foreach ($matches->find('tr[class=a1],tr[class=a2]') as $matchesTR) {

        $kickofftime=$matchesTR->find('td[class=a11],td[class=a21]',0)->plaintext;
        $kodate = date('Y-m-d H:i:s', strtotime("$matchday $kickofftime +1 hour"));
        $result=$matchesTR->find('td');

        echo $kodate;
        echo $result[6]->plaintext.
        $value = json_decode($json[$json_keys[$i_key++]], true);
        echo $value["country"] . ", " . $value["competition"] . "  " . $value["club"] . "<br>";
        $i++;
    }
}

// clean up memory
$html->clear();
unset($html);

还有很多其他变种 - 使用array_shift(),next()......我已经写好了第一个想法

相关问题