Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #7372
| Message-ID | <2044507.2AoWnWmAXe@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2012-10-28 21:09 +0100 |
| Subject | Re: Using += assignment recursively on an array w/o notice |
| Newsgroups | comp.lang.php |
| References | <k6jpko$7rr$1@dont-email.me> |
| Followup-To | comp.lang.php |
Followups directed to: comp.lang.php
Scott Johnson wrote:
> I am pulling data from a DB that represent products and product options.
>
> <ProductName>
> <Color>
> <Size>
> <Qty>
>
> The same Product name will represent several rows of color/size
> combinations with a qty amount.
>
> What I would like to be able to do is walk thru the returned data set
> returned and aggregate the qty from all the rows into a an array for
> later processing.
>
> what I tried was this:
>
> $item = array();
> foreach($products as $data) {
> $item[$data['ProductName']]['TotalQty'] += $data['Qty'];
> $item[$data['ProductName']]['data'] = $data;
If this occurs more often, I would consider using references for easier
maintenance:
$product_name = $data['ProductName'];
$item[$product_name] = array();
$a =& $item[$product_name];
$a['TotalQty'] += $data['Qty'];
$a['data'] = $data;
(However, consecutive execution of the last statement (optimized or not)
does not make sense. The `data' item of the array would always contain only
the data in the last record for the product.)
This also goes a long way towards avoiding the notice:
> }
>
> Now this does work and gives me the exact data I need in the right schema.
Are you sure about that?
> The problem I am running into is I am getting a Notice about undefined
> index since the first iteration of the += is looking for an array index
> yet to be declared.
>
> Now I figured a statement that works
>
> $item[$data['ProductName']]['TotalQty'] =
> isset($item[$data['ProductName']]['TotalQty']) ?
> $item[$data['ProductName']]['TotalQty'] + $data['Qty'] : $data['Qty'];
(Yuck. Are you sure you would know why you did this in a month from now?
Would you want to read that in other's code?)
$product_name = $data['ProductName'];
if (!array_key_exists($product_name, $item))
{
$item[$product_name] = array(
'TotalQty' => 0,
'data' => $data,
);
$a =& $item[$product_name];
}
$a['TotalQty'] += $data['Qty'];
(You could skip the reference definition here. And decide for either
lowercase or uppercase keys.) Untested.
> Is there a statement that will give me something simpler then this or is
> this just the way to do it.
First of all, ask yourself why you have to care about notices. They are
good for development but should be hidden in production, even from log
files, so as not to make a junk file.
Second, you can define what messages you see:
/* Disable notices */
error_reporting(error_reporting() ^ E_NOTICE);
$item[$data['ProductName']]['TotalQty'] += $data['Qty'];
/* Enable notices */
error_reporting(error_reporting() | E_NOTICE);
Third, you can suppress all notices, warnings and non-fatal errors that a
statement generates, with the `@' notation:
@$item[$data['ProductName']]['TotalQty'] += $data['Qty'];
Works for me. But I have found `@' to cause a syntax error with language
constructs like `echo' (@echo), and often you do not want warnings right-
hand side of an assignment suppressed. So be careful with that.
Fourth, using the same feature, you can make these assignments part of a
function – say, foo() – and call that function with
@foo();
to suppress all notices, warnings and non-fatal errors it would generate.
Because of that, you should limit the function body to the parts that you
actually want to be silent.
That said, I strongly suggest proper coding instead of suppressing notices.
They are there for a reason.
PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7@news.demon.co.uk> (2004)
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Using += assignment recursively on an array w/o notice Scott Johnson <noonehome@chalupasworld.com> - 2012-10-28 10:21 -0700
Re: Using += assignment recursively on an array w/o notice Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-28 21:09 +0100
Re: Using += assignment recursively on an array w/o notice Curtis Dyer <dyer85@gmail.com> - 2012-11-11 14:32 +0000
Re: Using += assignment recursively on an array w/o notice Denis McMahon <denismfmcmahon@gmail.com> - 2012-10-28 21:37 +0000
csiph-web