/    Sign up×
Community /Pin to ProfileBookmark

Key/Value Question..

Hello.

I’m a newbie to PHP..

I have a problem that asks for me to [I]create a program that reads a file that has a list of emails and the program counts the number of times an email is repeated, then write the emails and the number of times it was repeated to another file line by line in ascending order based on the number of times each email was repeated (using arsort()).[/I]

Now, my solution was to run a double loop and save the email and the number of times it was repeated to an array as an associative array.

Like so..

[code=php]
[U]addresses.txt[/U]
[email protected]

[U]code[/U]
<?php

$file_access=fopen(“address.txt”, rb);

if (!$file_access){print “some error message:$php_errormsg”;}

else
{

for ($line=fgets($file_access); !feof($file_access); $line=fgets($file_access))
{
$count=0;

if ($line===false){print “some error message:$php_errormsg”;}
else
{

for ($i=fgets($file_access); !feof($file_access); $i=fgets($file_access))
{
if ($line==$i) {$count++;}
}

$email_array=array(’email’ -> $line,
‘count’ -> $count);

}
}

…some rest of code

[/code]

Well, the book answer says…

[code=php]
<?php

$in_fh = fopen(‘address.txt’, ‘rb’);

if (! $in_fh){
die(“can’t open addresses.txt:$php_errormsg”);

}

$addresses=array();

for ($line = fgets($in_fh); ! feof($in_fh); $line = fgets($in_fh)) {

if ($line === false){

die(“Error reading line:$php_errormsg”);

} else {
$line = trim($line);
$addresses[$line] = $addresses[$line]+1;

}

}

if (! fclose($in_fh)) {

die(“Can’t close addresses.txt:$php_errormsg”);

}

..some rest of code…

[/code]

I don’t understand this line[I] $addresses[$line] = $addresses[$line]+1;[/I]..How was this line able to create an associative array..?…

The book’s explanation for was: [I][COLOR=”Purple”]Use the address as the key in $addresses and the value is the number of times that the address has appeared[/COLOR][/I]

How can the line in red evaluate to [COLOR=”Purple”]$addresses[$foo_email]=3[/COLOR]..?..

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiMay 05.2010 — It's like the book's explanation says - The element of the array with the email address as its key has as its value the number of times the address appears. Each time the same email address comes up, the value at that key has 1 added to it. So imagine you have [email][email protected][/email]. The first time it comes up, after your loop, you will have an element like this:

Array( '[email protected]' =&gt; 1)

The next time, you will have this:

Array( '[email protected]' =&gt; 2)

because you will be taking the previous value (1) and adding 1 to it. And this continues until the end.

However the code from the book is pretty bad in that it will produce notice level errors - one for each time an email address is first encountered since it references an element of the array which doesn't exist. In other words, the first time through the loop this code tries to add 1 to something which doesn't exist. PHP is clever enough to guess that "something which doesn't exist plus one is one", but it will throw an error telling you about your mistake. Pretty sloppy for a tuition book!

Just so you know, this can be done much simpler like this:

[code=php]
// read the email addresses into an array
$emails = file('address.txt');
// count the frequency of each email address
$frequencies = array_count_values($emails);
// sort the results
arsort($frequencies);
// write the results to a file
$lines = array();
foreach ($frequencies as $email => $frequency) {
$lines[] = "$email: $frequencyn";
}
file_put_contents('results.txt', $lines);[/code]


I expect the point is to teach you about manual file reading, but in reality I almost never do it that way because its more complicated than it needs to be (the one exception I can think of being reading very large files which is better done line by line). There are already much more convenient functions built in so it makes sense to use them.
Copy linkTweet thisAlerts:
@ChuckBauthorMay 09.2010 — 

I expect the point is to teach you about manual file reading, but in reality I almost never do it that way because its more complicated than it needs to be (the one exception I can think of being reading very large files which is better done line by line). There are already much more convenient functions built in so it makes sense to use them.[/QUOTE]


Thanks Mindzai for the tutorial...sorry for the late response...

As you can see I am new to this language, coming from JavaScript. As I am discovering more and more, there are built-in methods that my lesson book isn't referencing. Where can I get an up-to-date listing of PHP built-in methods?
Copy linkTweet thisAlerts:
@NogDogMay 09.2010 — Thanks Mindzai for the tutorial...sorry for the late response...

As you can see I am new to this language, coming from JavaScript. As I am discovering more and more, there are built-in methods that my lesson book isn't referencing. Where can I get an up-to-date listing of PHP built-in methods?[/QUOTE]


[url=http://www.php.net/manual/en/index.php]Official on-line PHP manual[/url]

[url=http://www.php.net/manual/en/funcref.php]Function Reference section[/url]

[url=http://www.php.net/manual/en/ref.array.php]Array Functions section[/url]
Copy linkTweet thisAlerts:
@ChuckBauthorMay 10.2010 — [url=http://www.php.net/manual/en/index.php]Official on-line PHP manual[/url]

[url=http://www.php.net/manual/en/funcref.php]Function Reference section[/url]

[url=http://www.php.net/manual/en/ref.array.php]Array Functions section[/url][/QUOTE]


thanks a million for this...
×

Success!

Help @ChuckB spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...