473,387 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help with arrays

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
5 1867
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dariusz | last post by:
I want to use arrays in my website (flat file for a guestbook), but despite having read through countless online tutorials on the topic, I just can't get my code to work. I know there are...
4
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it...
4
by: Mingus Tsai | last post by:
Hello- please help with unpickling problem: I am using Python version 2.3.4 with IDLE version 1.0.3 on a Windows XPhome system. My problem is with using cPickle to deserialize my pickled...
8
by: inkexit | last post by:
I am a very amatuer c++ programmer and a somewhat accomplished composer. I am trying to write some code that creates 'self similar' melodies from a base melody the user inputs. This musical idea...
1
by: Geoff | last post by:
I was wondering if anyone could help me with a problem I am having. I am trying to read in a list of numbers from a file and then sort them using pointers and malloc() and free(). I know how to...
2
by: Pasacco | last post by:
dear I want to ask help on this problem. Array a is partitioned into a0 and a1 in main(). Then a1 is partitioned into a2 and a3 in th_partition() function. And I think this problem is something...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
2
by: Dr Dav | last post by:
Hello all, I'm a physicist whose rewriting a numerical simulation, previously written in IDL, in C with the goal reducing runtime. As you may imagine, my C programming skills are quite poor but I...
5
by: saytri | last post by:
Hi i have this project were i have to do a quiz. i wrote the questions in a textfile and i called them through java. I have also made a menu to choose which type of quiz. But before accessing the...
110
by: fjm | last post by:
For some reason, I have always had a hard time understanding arrays as they pertain to php and databases. I understand associative arrays just fine but when there are multidimensional arrays, I kinda...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.