php - Q: How can I combine these two foreach loop -


i have simple html dom query read informations football fixtures source, , loading json source.

here full code:

<?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, 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') $matches)  {      foreach ($matches->find('tr[class=a1],tr[class=a2]') $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 2nd foreach data of json source:  foreach($json $key => $value) {      $value = json_decode($value, true);     echo $value["country"] . ", " . $value["competition"] . "  " . $value["club"] . "<br>"; }  // clean memory $html->clear(); unset($html);  ?> 

the current results simple html dom html source:

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

the result json source:

america copa america boca juniors
europe bundesliga hannover
asia jleague nagoya

i combine these 2 results in 1 foreach , result:

2014-12-23 20:00:00 2-1 america copa america boca juniors
2014-12-23 11:00:00 3-1 europe bundesliga hannover
2014-12-26 08:00:00 1-1 asia jleague nagoya

i hope there expert can me because tried lot of variation without result. got advice (with code) experts, there everytime errors. code there no error, need other solution because put variables 1 foreach. many thanks, hope me code, because not on high level @ php. again!

i put 2 foreach 1 foreach, don't want create new array because not need.

assuming have same amount of items in each , match 1 1, 2 2, can this:

$htmldates = array(); $jsonleag = array();  foreach($html->find('table.fixtures') $matches) {     foreach($matches->find('tr[class=a1],tr[class=a2]') $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 2nd foreach data of json source:  foreach($json $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";     } } 

since have nested list first , nothing ties 2 data sets together, there no simple way run 1 loop , data both. it's easier push data want array each set, , walk both arrays 1 counter. caveat here if 1 has element, missing results or errors.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -