dinamic hashes
| From: | Yivi | Date: | Thu, 13 May 2004 11:59:23 +0000 |
| Subject: | dinamic hashes | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-186111@lists.php.net to get a copy of this message | ||
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"]++;
"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();
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
Notice: Undefined index: www.google.com in c:\www\deepswarm.co.uk\script\monthlytable.php on line 60
Notice: Undefined index: obscuredterm in c:\www\deepswarm.co.uk\script\monthlytable.php on line 60
Notice: Undefined index: total in c:\www\deepswarm.co.uk\script\monthlytable.php on line 60
Notice: Undefined index: count in c:\www\deepswarm.co.uk\script\monthlytable.php on line 62
Using the "@" operator the scripts works perfectly, but I still want to know why I am generating so many notices, and if there is any other "cleaner" way of doing things.
I can't create the hash with all necessary keys beforehand because I won't know the values until runtime, so doing something like:
$table = array ("GGAW" =>
array ("www.google.com" =>
array ("obscuredterm" =>
array("total", "count")
)
)
)
before going into the actual loop is out of the question.
Any ideas? This is probably very obvious, but I keep hitting my head against the wall. Light would be greatly appreciated.
Thanks for your help.
Regards,
I.-