Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #7370

Using += assignment recursively on an array w/o notice

From Scott Johnson <noonehome@chalupasworld.com>
Newsgroups comp.lang.php
Subject Using += assignment recursively on an array w/o notice
Date 2012-10-28 10:21 -0700
Organization A noiseless patient Spider
Message-ID <k6jpko$7rr$1@dont-email.me> (permalink)

Show all headers | View raw


Hello

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;
}

Now this does work and gives me the exact data I need in the right schema.

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'];


Is there a statement that will give me something simpler then this or is 
this just the way to do it.

Just curious.

Thanks
Scotty

Back to comp.lang.php | Previous | Next — Next in thread | Find similar | Unroll thread


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