By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,724 Members | 1,868 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,724 IT Pros & Developers. It's quick & easy.

Help with arrays

P: n/a
Hi,
I have problems in trying to '++' an array value.
Every time I find a city I add it in the array,
and if I find it many times I increment 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.
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Shmuel wrote:
(snip)
if(in_array($town, $towns)) {
Shouldn't this be reversed?

if(!in_array($town, $towns)) {
$towns[$town] = 1;
echo "Town added: $town<br>";

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

(snip)
--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #2

P: n/a
Then the result will be 1 for each town even if there are many hits.
Pedro Graca wrote:
Shmuel wrote:
(snip)
if(in_array($town, $towns)) {

Shouldn't this be reversed?

if(!in_array($town, $towns)) {

$towns[$town] = 1;
echo "Town added: $town<br>";

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


(snip)

Jul 17 '05 #3

P: n/a
Shmuel wrote:
Then the result will be 1 for each town even if there are many hits.

use array_key_exists() instead of in_array()

http://www.php.net/array_key_exists

========
pedro$ cat xx.php
<?php
$towns = array();
$towns['x'] = 3;

$town = 'x';
if(!array_key_exists($town, $towns)) {
$towns[$town] = 1;
echo "Town added: $town<br>\n";
} else {
$towns[$town]++;
echo "Town added: $town<br>\n";
}

$town = 'y';
if(!array_key_exists($town, $towns)) {
$towns[$town] = 1;
echo "Town added: $town<br>\n";
} else {
$towns[$town]++;
echo "Town added: $town<br>\n";
}

print_r($towns);
?>

pedro$ php xx.php
Town added: x<br>
Town added: y<br>
Array
(
[x] => 4
[y] => 1
)
========

--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #4

P: n/a
Shmuel wrote:
Pedro Graca wrote:
Shouldn't this be reversed?


Then the result will be 1 for each town even if there are many hits.


No, Pedro is right - your code is wrong, or isn't it 1 for each town
even if there are many hits?

Greetings Christian
Jul 17 '05 #5

P: n/a
Do this:

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

$pos = strpos($lines[$i], "Location: ");
if($pos !== false) {
list($loc,$town) = preg_split("/: /", $lines[$i]);
@$towns[$town] += 1;
}

}
"Shmuel" <sd*@nic.fi> wrote in message
news:lE****************@reader1.news.jippii.net...
Hi,
I have problems in trying to '++' an array value.
Every time I find a city I add it in the array,
and if I find it many times I increment 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?


Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.