Re: Using += assignment recursively on an array w/o notice [message #179439 is a reply to message #179437] |
Sun, 28 October 2012 20:09 |
Thomas 'PointedEars'
Messages: 701 Registered: October 2010
Karma:
|
Senior Member |
|
|
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(at)news(dot)demon(dot)co(dot)uk> (2004)
|
|
|