RE: [PHP] dinamic hashes
| From: | Ford, Mike [LSS] | Date: | Thu, 13 May 2004 16:13:22 +0000 |
| Subject: | RE: [PHP] dinamic hashes | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-186150@lists.php.net to get a copy of this message | ||
On 13 May 2004 12:59, Yivi wrote:
> Hello everyone.
> I am having a stupid problem with a couple of arrays, tried a
> couple of
> things but I am feeling disconcerted.
>
> The thing is, I thought it was possible to create arrays in a dynamic
> fashion, as in: $newarray['newkey'] = "new_value";
> And the array would be incremented accordingly.
> And afterwards, if I wanted to add a new key to the thing, just doing:
> $newarray['anotherkey'] = "anothervalue"; would be enough.
> Generally speaking I have been trying that and it was
> working, but right
> now I am having some trouble with this.
>
> I am parsing a kind of referrals log file, and as I get the values I
> want to create a hash with all the values I get into a hash to use
> later on.
>
> Each line of the log is processed in a while() loop, and after
> extracting the values for $searchSource, $searchDomain and
> $searchTerm I try to create the hash entries with these lines:
>
>
> $table["$searchSource"]["$searchDomain"]["$searchTerm"]["total
> "]+=$row['order_total'];
>
> $table["$searchSource"]["$searchDomain"]["$searchTerm"]["count"]++;
Your problem here is that you are using the += and ++ operators, which need
to fetch the existing value of your array element before adding to or
incrementing them. Of course, the *first* time you attempt to do this the
array element doesn't exist yet, so PHP throws a notice for each of the
subscripts; subsequent additions to or increments of the same elements will
proceed without problem.
> "total" is an amount that I want to aggregate for each $seachTerm, and
> "count" an obvious counter with total hits per
> $searchTerm/Domain/Source
> combination. $table is an array which I create before
> entering the loop
> with: $table = array();
This is irrelevant and, strictly speaking, unnecessary.
> The problem is that I keep getting this warnings and notices:
> Notice: Undefined index: GGAW in
> c:\www\deepswarm.co.uk\script\monthlytable.php on line 60
[...]
> Using the "@" operator the scripts works perfectly, but I
That's one of the obvious ways to suppress these notices. Others are to
turn off E_NOTICE level messages for the duration, or use isset() on each
access to decide whether to do a plain assignment or use an
increment/addition.
BTW, all those quotes are unnecessary and inefficient -- just write your
array accesses like this:
$table[$searchSource][$searchDomain][$searchTerm]["count"]
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211