为什么数组结果重复?

时间:2015-11-19 05:46:50

标签: php arrays loops foreach

任何人都可以帮助测试此代码并告诉我错误是什么吗? 预期结果为01223658060102111111。但它像这样重复

01223658060102111111012236580601021111110122365806010211111101223658060102111111

这是我的代码

<?php
ini_set('user_agent', 'My-Application/2.5'); //without this file_get_content would not work
$saveURL = fopen("url.txt", "w");
$html = file_get_contents("http://www.carlist.my/used-cars/2592832/2004-toyota-camry-2-0.html");
    $dom = new DOMDocument();
    @$dom->loadHTML($html);
    $xpath = new DOMXPath($dom);


   $cont = $xpath->evaluate("//ul[contains(@class, 'list-contact')]/li");
   foreach($cont as $con){
    echo $con->nodeValue;
   }

2 个答案:

答案 0 :(得分:1)

将此行替换为

$cont = $xpath->evaluate("//ul[contains(@class, 'list-contact')]/li");

$cont = $xpath->evaluate("//ul[contains(@class, 'list-contact')][1]/li");

答案 1 :(得分:1)

  

@ july77:如何拆分它们以获得结果0122365806和060102111111

   $arr = array();
   foreach($cont as $con){
    $arr[] = $con->nodeValue;
   }
   $first = $arr[0];
   $second = $arr[1];