473,583 Members | 2,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.t xt";
$lines = file($address_l ocal);

$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($to wn, $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\wor k\jobs\index.ph p on line 40
Town added: Ramat Hasharon

Notice: Undefined index: North in C:\Programs\wor k\jobs\index.ph p on line 40
Town added: North
Town added: North

Notice: Undefined index: Location in C:\Programs\wor k\jobs\index.ph p 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 1880
Shmuel wrote:
(snip)
if(in_array($to wn, $towns)) {
Shouldn't this be reversed?

if(!in_array($t own, $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($to wn, $towns)) {

Shouldn't this be reversed?

if(!in_array($t own, $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_exist s() 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_e xists($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_e xists($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******** ********@reader 1.news.jippii.n et...
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.t xt";
$lines = file($address_l ocal);

$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($to wn, $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\wor k\jobs\index.ph p on line 40
Town added: Ramat Hasharon

Notice: Undefined index: North in C:\Programs\wor k\jobs\index.ph p on line 40 Town added: North
Town added: North

Notice: Undefined index: Location in C:\Programs\wor k\jobs\index.ph p 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
3447
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 guestbook scripts out there - but that doesn't help me learn how to programme arrays !!! The following is the code for the PHP (called externally),...
4
1829
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 elaborate to make sure my questions are clear enough. Let's say I need to write a function whose logic is same for all types (T) except in the case...
4
2815
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 arrays of datetime.datetime instances. The following is the code I have written: import cPickle, datetime import Numeric
8
2251
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 was created by Tom Johnson. Apologies to those who are not musically literate, but as this is basically a mathmatical problem I think most here...
1
455
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 do it via arrays but I am stumbled on how to implement this same solution with pointers, "malloc" and "free" as I can't use arrays. Below is the...
2
1622
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 about parameter passing. If someone give me comment, it will be thankful. thankyou very much
9
2494
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 *after* calling increase_arrays(), I received segmentation fault. I tried to step it through gdb, and I found out that after calling...
2
1997
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 am learning. My problem is this. I had sucessfully written a C code that worked ( with considerable speedup over the IDL version ) using an array...
5
2148
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 quiz i have to do a password and a login. I managed to do the password but when i tried to join this to the whole program its not working. This is the...
110
6915
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 don't. I have gone over a few different examples but they were limited. I was able to find one piece of code that I would like to disect and ask...
0
7896
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8186
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8329
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6583
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3821
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3847
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2335
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1436
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1160
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.