2011-11-12 34 views
1

我有这样一段代码:故障排除“通知:未定义指数”,但指数似乎被定义

<?php 

$pattern = "/<span\b[^>]*>(.*?)</"; 
$html = file_get_contents("http://www.bicicletapublica.com.ar/mapa.aspx"); 
$results = array(); 
preg_match_all($pattern, $html, $results); 
$results = $results[0]; 
$stations = array(); 

for($i = 0; $i < count($results)-1;$i++){ 
    if(!($i & 1)){ 
     $key = strtolower(str_replace(" ","_",substr($results[$i], 0, -1))); 
     $stations["$key"] = str_replace("Cant. Bicicletas disponibles: ","",substr($results[$i+1], 0, -1)); 

    } 
} 
print_r($stations); 
print_r($stations["retiro"]) 
?> 

而且我得到这个错误:

Array ([retiro] => 40 [aduana] => 26 [derecho] => 27 [plaza_roma] => 8 [plaza_italia] => 29 [parque_lezama] => 31 [obelisco] => 28 [congreso] => 7 [parque_las_heras] => 17 [uca_puerto_madero] => 27 [tribunales] => 25 [plaza_vicente_lopez] => 23 [once] => 27 [pacifico] => 19 [virrey_cevallos] => 26 [plaza_houssay] => 2 [plaza_de_mayo] => 6 [plaza_almagro] => 21 [cmd] => 7 [independencia] => 9 [plaza_san_martin] => 21) 

Notice: Undefined index: retiro in /opt/lampp/htdocs/mejorenbici/index.php on line 17 

正如你所看到的,密钥retiro已定义,但我不明白为什么会触发未定义的索引错误。

+1

这里有更多故事。 PHP文件中的任何有趣的字符?你没有在你的阵列上使用unicode索引,对吧?此外,不需要在'$ stations [“$ key”] = str_replace(...'。 – Brad

+0

@Brad - >当我在我的网站上运行该代码时,我也会得到相同的错误数组中的所有按键 –

+0

@JaredFarrish,PointedEars在下面列出了它,对于Topicus和你来说,不要给我们错误,就像浏览器呈现的那样......还有更多的东西,那是隐藏的,查看源显示 – Brad

回答

3

看那通知HTML源代码:错误)。当php -a运行在控制台你的代码,我得到:

Array (
    [<span_class="style1">retiro] => <span class="style2">40 
    [<span_class="style1">aduana] => <span class="style2">26 
    [<span_class="style1">derecho] => <span class="style2">27 
    [<span_class="style1">plaza_roma] => <span class="style2">8 
    [<span_class="style1">plaza_italia] => <span class="style2">29 
    [<span_class="style1">parque_lezama] => <span class="style2">31 
    [<span_class="style1">obelisco] => <span class="style2">28 
    [<span_class="style1">congreso] => <span class="style2">7 
    [<span_class="style1">parque_las_heras] => <span class="style2">17 
    [<span_class="style1">uca_puerto_madero] => <span class="style2">27 
    [<span_class="style1">tribunales] => <span class="style2">25 
    [<span_class="style1">plaza_vicente_lopez] => <span class="style2">23 
    [<span_class="style1">once] => <span class="style2">27 
    [<span_class="style1">pacifico] => <span class="style2">19 
    [<span_class="style1">virrey_cevallos] => <span class="style2">26 
    [<span_class="style1">plaza_houssay] => <span class="style2">2 
    [<span_class="style1">plaza_de_mayo] => <span class="style2">6 
    [<span_class="style1">plaza_almagro] => <span class="style2">21 
    [<span_class="style1">cmd] => <span class="style2">7 
    [<span_class="style1">independencia] => <span class="style2">9 
    [<span_class="style1">plaza_san_martin] => <span class="style2">21 
) 
PHP Notice: Undefined index: retiro in - on line 19 
PHP Stack trace: 
PHP 1. {main}() -:0 

不要只应用一个正则表达式只有一次解析HTML;使用标记解析器(可以使用正则表达式来提高效率)

您是否有权重用该数据?

+0

啊哈!他没有给我们明文错误,但浏览器呈现版本。良好的通话! – Brad

+0

非常感谢回复!是的,这是问题。以错误的方式获取数据。 – Topicus

1

你应该包裹站点数组以确保你没有在数组中输入空索引。您的错误可能是由以前的print_r()语句引起的:

print_r($ stations);

新的逻辑包装:

if (!empty($key)) { $stations["$key"] = str_replace("Cant. Bicicletas disponibles: ","",substr($results[$i+1], 0, -1)); } 

这应该防止空索引您的阵列英寸