Help with arrays

This is a discussion on Help with arrays within the PHP General forums, part of the PHP Programming Forums category; Hi, I have problems in trying to '++' an array value. Every time I find a city I add it to ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-11-2004
Shmuel
 
Posts: n/a
Default Help with arrays

Hi,
I have problems in trying to '++' an array value.
Every time I find a city I add it to an array,
and if I find it many times I add the value (counter)
of the city. Here's the code:


$address = "http://www.cji.co.il/bw040609.txt";
$address_local = "bw040609.txt";
$lines = file($address_local);

$heading = "Companies covered in this CJI report:";
$heading_line = 0;

$towns = array();

for($i=0; $i<count($lines); $i++) {

$pos = strpos($lines[$i], "Location: ");
if($pos !== false) {
list($loc,$town) = preg_split("/: /", $lines[$i]);
if(in_array($town, $towns)) {
$towns[$town] = 1;
echo "Town added: $town<br>";

}
else {
$towns[$town]++;
echo "Town added: $town<br>";
}
}

}


foreach($towns as $key => $value) {
print "$key => $value<br>";
}



The output is like this:

Notice: Undefined index: Ramat Hasharon in
C:\Programs\work\jobs\index.php on line 40
Town added: Ramat Hasharon

Notice: Undefined index: North in C:\Programs\work\jobs\index.php on line 40
Town added: North
Town added: North

Notice: Undefined index: Location in C:\Programs\work\jobs\index.php on
line 40
Town added: Location
Town added: Location
Town added: North
Town added: North

and so on...

It complains about an undefined index.
Any idea why it does that?

Shmuel.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 01:18 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0