FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Using += assignment recursively on an array w/o notice
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Using += assignment recursively on an array w/o notice [message #179437] Sun, 28 October 2012 17:21 Go to previous message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma:
Senior Member
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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Previous Topic: Problem with mysqli_stmt_bind_param()
Next Topic: Message queues not working properly PHP -> C
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 11:54:43 GMT 2024

Total time taken to generate the page: 0.06920 seconds