1

I have this piece of code:

<?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"])
?>  

And I'm getting this error:

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

As you can see, the key retiro was defined, but I don't understand why the undefined index error is triggered.

Tadeck
  • 132,510
  • 28
  • 152
  • 198
Topicus
  • 1,394
  • 1
  • 15
  • 27
  • 1
    There has to be more to the story here. Any funny characters in your PHP file? You're not using unicode indices on your array, right? Also, no need to put `$key` in quotes at `$stations["$key"] = str_replace(...`. – Brad Nov 12 '11 at 21:51
  • @Brad - When I run that code on my site, I also get the same error on all of the keys in the array. – Jared Farrish Nov 12 '11 at 21:59
  • @JaredFarrish, PointedEars figured it out below. For Topicus and you, do not give us errors as rendered by a browser... there was more to it, that was hidden, and viewing source revealed it. – Brad Nov 13 '11 at 02:47
  • @Brad - "For Topicus and you" What does this mean? That comment had to do with your initial comment, and as I was further researching the issue (after the comment), PointedEars posted his answer (which I have already upvoted). So I don't know what you mean by "do not give us errors" and you seem to be talking to me to. – Jared Farrish Nov 13 '11 at 04:05

2 Answers2

3

Look at the HTML source code of the notice (not: error). When running your code in the console with php -a, I am getting:

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

Do not parse HTML by applying only one Regular Expression only one time; use a markup parser (which can make use of Regular Expressions for greater efficiency).

Do you have permission to reuse that data?

PointedEars
  • 14,752
  • 4
  • 34
  • 33
  • Aha! He didn't give us the plain-text error, but the browser-rendered version. Good call! – Brad Nov 13 '11 at 02:46
  • Thanks a lot for the reply! yes, that was the problem. It was parsing in a wrong way the data. – Topicus Nov 14 '11 at 16:28
1

you should wrap the stations array to make sure you're not entering empty indexes in your array. your error is probably caused by the previous print_r() statement:

print_r($stations);

new logic wrapping:

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

this should prevent empty indexes in your array.

Robert Van Sant
  • 1,497
  • 10
  • 12