duannaoben8011 2013-05-10 17:27 采纳率: 0%
浏览 50
已采纳

在foreach只有回声的最后一项?

I am trying to extract download links from a site. However i only get the last item in my array.

<?php
    require 'functions/simple_html_dom.php';
    $html = new simple_html_dom();
    $html->load_file('http://www.nyaa.eu/?page=torrents&user=64513');
    $page_title = $html->find('title',0);
    ?>

    Title:<?php echo $page_title->plaintext; ?><br><br>
    Links:<br>
    <?php
    foreach($html->find('td.tlistdownload a') as $links){
        $dllinks[] = $links->href;
    }
    foreach($html->find('td.tlistname a') as $names){
        echo '<a href="'; 
        foreach ($dllinks as $value)
        {
            echo $value;
        }
        echo '">' . $names->innertext . '</a><br>';
    }
    foreach ($dllinks as $value)
    {
        echo $value . '<br>';
    } 
?>

When I use var_dump it shows all the download links in my array. But for some strange reason it only shows the last item in the second foreach loop.


EDIT:
Sorry it was supposed to be like this

    <?php
    require 'functions/simple_html_dom.php';
    $html = new simple_html_dom();
    $html->load_file('http://www.nyaa.eu/?page=torrents&user=64513');
    $page_title = $html->find('title',0);
    ?>

    Title:<?php echo $page_title->plaintext; ?><br><br>
    Links:<br>
    <?php
    foreach($html->find('td.tlistdownload a') as $links){
        $dllinks[] = $links->href;
    }
    foreach($html->find('td.tlistname a') as $names){
        echo '<a href="'; 
        foreach ($dllinks as $value)
        {
            echo $value;
        }
        echo '">' . $names->innertext . '</a><br>';
    }
?>
  • 写回答

1条回答 默认 最新

  • douxue7196 2013-05-10 17:44
    关注

    I kept this verbose so its easier to see whats going on... Basically, grab each row.. Find the name and the link from the row. spit it out..

    <?php
    require 'functions/simple_html_dom.php';
    $html = new simple_html_dom();
    $html->load_file('http://www.nyaa.eu/?page=torrents&user=64513');
    $page_title = $html->find('title',0);
    ?>
    
    Title:<?php echo $page_title->plaintext; ?><br><br>
    Links:<br>
    <?php
    foreach($html->find('.tlistrow') as $row){
        $link_nodes = $row->find('td.tlistdownload a');
        $name_nodes = $row->find('td.tlistname a');
        if (count($link_nodes) > 0 && count($name_nodes) > 0) {
          $link = $link_nodes[0]->href;
          $name = htmlentities($name_nodes[0]->innertext);
          echo "<a href='{$link}'>{$name}</a>
    ";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题