Php 为什么数组结果是重复的?
有人能帮我测试一下这段代码并告诉我错误是什么吗? 预期结果为Php 为什么数组结果是重复的?,php,arrays,loops,foreach,Php,Arrays,Loops,Foreach,有人能帮我测试一下这段代码并告诉我错误是什么吗? 预期结果为0122365806010211111。但它是这样复制的 01223658060102111111012236580601021111110122365806010211111101223658060102111111 这是我的密码 <?php ini_set('user_agent', 'My-Application/2.5'); //without this file_get_content would not work $
0122365806010211111
。但它是这样复制的
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;
}
将此行替换为
$cont=$xpath->evaluate(//ul[contains(@class,'list contact'))]/li)代码>
到
$cont=$xpath->evaluate(//ul[contains(@class,'list contact')][1]/li”)代码>
@七月77日:我如何拆分它们以得到结果0122365806和06010211111
尝试echo$con->nodeValue。“
”
查看您的foreach循环中的echo是否实际运行了3次使用unset$con
在echo之后为什么需要foreach
?只需添加中断;在echo@jitendrapurohit之后;因为我需要li@Haktan Suren内部的所有元素;我想将0122365806和06010211111插入数据库中的两个字段(contact1、contact2)。您能告诉我一些想法吗?我如何分割它们以得到结果0122365806和06010211111您只需要修改foreach循环,如$arr=array();foreach($contas$con){$arr[]=$con->nodeValue;}$first=$arr[0]$秒=$arr[1];如果li元素发生变化怎么办?有时它有1个、2个和3个或3个以上的联系人号码?我们可以解决这个问题吗?您可以循环使用$arr数组,但不能以这种方式处理数据库。您需要更好的策略将数据保存在数据库中。例如,您可以将它们保存在一个分隔的字段中,例如field1 | field2 | field3等。。。您可以使用explode(“|”,$string)访问每个单独的项目。当然,这取决于您想要如何实现它。
$arr = array();
foreach($cont as $con){
$arr[] = $con->nodeValue;
}
$first = $arr[0];
$second = $arr[1];