【问题标题】:Q: How can I combine these two foreach?问:如何将这两个 foreach 结合起来?
【发布时间】:2015-08-11 22:36:30
【问题描述】:

我有一个简单的 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 $title;


        function matches ($day, $kickofftime, $tip){
                    $this->day=$day;
            $this->kickofftime=$kickofftime;
                    $this->title=$title;
            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 源的结果:

美国美洲杯博卡青年队
欧洲德甲汉诺威
亚洲J联赛名古屋

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

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

我希望有一些专家可以帮助我,因为我尝试了很多变化但没有结果。非常感谢!

【问题讨论】:

  • 可以使用for循环右移

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


【解决方案1】:
     $j=0; 
 $jsonDecodedArray=$json;  

 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." "; 
        echo $jsonDecodedArray[$j]["country"] . ", " . $jsonDecodedArray[$j]["competition"] . "  " . $jsonDecodedArray[$j]["club"] . "<br>"; ;

        $i++;
        $j++;


    }
        }

【讨论】:

  • 感谢您的回答。但是为什么要使用第二个 json 解码呢?是不是第一个json解码?
  • json_decode() 期望参数 1 为字符串,数组给定 - 我在这一行收到此错误消息:$jsonDecodedArray=json_decode($json);
  • 您的代码有问题,因为我每次都收到错误消息。使用我的代码没有错误
  • 感谢您的帮助,但现在此行出现 Illegal string offset 错误消息: echo $jsonDecodedArray[$j]["country"] 。 “,”。 $jsonDecodedArray[$j]["competition"] 。 “”。 $jsonDecodedArray[$j]["club"] 。 "
    ";
  • 我已经尝试了很多变化,但没有成功。使用 2 json 解码时,我收到关于 json_decode() 的错误,预计参数 1 是字符串,给定数组,如果我删除了其中一个 json_decode,那么在使用您的代码后,我会收到相同的错误消息
猜你喜欢
  • 2015-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-23
  • 2021-09-05
  • 2021-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多