2015-05-29 19 views
1

我有一個簡單的html dom查詢,它從一個足球裝置源讀取信息,並且我也加載了一個json源代碼。結合兩個foreach沒有錯誤

這裏是我的全碼:

<?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, $result){ 
      $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源目前的結果:

情節中字20:00:00 2-1
2014- 12-23 11:00:00 3-1
2014-12-26 8點00分○○秒1-1

從JSON源結果:

美國美洲盃博卡青年
歐洲德甲漢諾威
亞洲JLeague名古屋

我想這兩個結果在一個的foreach結合,我希望得到這樣的結果:

2014-12-23 20:00:00 2-1美洲盃美洲博卡青年隊
2014-12-23 11:00:00 3-1歐洲德甲漢諾威
2014-12-26 08:00:00 1-1亞洲J聯賽名古屋

我希望能幫助我,因爲我嘗試了很多變化,但沒有結果。非常感謝!

+0

Arrggghhh - 不要使用simple_html_dom - 這是可怕的,PHP的DOMDocument好多了,只是設置libxml_use_internal_errors爲真 –

回答

0

您可以使用PHP的array_map函數。

比方說你的價值觀是數組$ A,$ B:

$mapFunc = function($n, $m) { 
    return "$n $m"; 
}; 
$c = array_map($mapFunc, $a, $b);