473,915 Members | 7,703 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 1899
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
3464
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), which does execute...
4
1854
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 of T * (including const T *). Furtheremore , the function needs to be written differently for...
4
2837
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
2268
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 can follow the logic. Lets keep eveything in C Major, that's all the white keys on the piano....
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 code I have to do it with arrays. If anyone can help give me some tips on how to implement this code...
2
1641
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
2525
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 increase_arrays(), words's original value is modified, and if I tried to access it, I get <address 0x11 out of...
2
2014
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 size of 100x100. However, I am working towards making the array size closer to 300x300. Since I had...
5
2163
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 main program: import java.util.*; import java.io.*; import java.util.Scanner; import...
110
7090
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 questions about so I can gain a better understanding. $characters = array ( array (...
0
10039
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9883
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10928
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4779
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
2
4346
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3370
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.