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
Re: Using += assignment recursively on an array w/o notice [message #179440 is a reply to message #179437] Sun, 28 October 2012 21:37 Go to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
Senior Member
On Sun, 28 Oct 2012 10:21:51 -0700, Scott Johnson wrote:

> $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.

$item = array();
foreach($products as $data) {
if ( !isset( $item[$data['ProductName']] ) ) {
$item[$data['ProductName']] = array();
$item[$data['ProductName']]['TotalQty'] = 0;
}
$item[$data['ProductName']]['TotalQty'] += $data['Qty'];
$item[$data['ProductName']]['data'] = $data;
}

However, I see a possible issue in your initial loop and my modified
version!

Given that you may have multiple $data with the same ['ProductName'],
what value ends up in:

$item[$data['ProductName']]['data']

because I think it gets over-written every time you find another product
that matched the product name, in which case, why are you bothering to
collect it at all? For each 'ProductName' it will simply contain the last
found "<ProductName> <Color> <Size> <Qty>" array for the last found
$products[] that matched that ProductName!

Consider:

Widget red large 7
Widget blue huge 8
Widget green large 5
Widget yellow medium 6
widget purple small 3
widget green tiny 4
widget yellow huge 2

Your final values for $item['widget'] would I think look like this:

$item['widget']['TotalQty'] = 35
$item['widget']['data'] = array( "widget", "yellow", "huge", 2 );

Perhaps you actually want something more like:

$item = array();
foreach($products as $data) {
if ( !isset( $item[$data['ProductName']] ) ) {
$item[$data['ProductName']] = array();
$item[$data['ProductName']]['TotalQty'] = 0;
$item[$data['ProductName']]['data'] = array();
}
$item[$data['ProductName']]['TotalQty'] += $data['Qty'];
$item[$data['ProductName']]['data'][] = array( $data['Color'],
$data['Size'], $data['Qty'] );
}

Where for each ProductName:

$item[$data['ProductName']]['TotalQty'] => Total Qty
$item[$data['ProductName']]['data'] => array of individual color / size /
qty info

Giving eg:

$item['widget']['TotalQty'] = 35
$item['widget']['data'][0] = array( "red", "large", 7 );
$item['widget']['data'][1] = array( "blue", "huge", 8 );
$item['widget']['data'][2] = array( "green", "large", 5 );
$item['widget']['data'][3] = array( "yellow", "medium", 6 );
$item['widget']['data'][4] = array( "purple", "small", 3 );
$item['widget']['data'][5] = array( "green", "tiny", 4 );
$item['widget']['data'][6] = array( "yellow", "huge", 2 );

Rgds

Denis McMahon

(untested code)
[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 15:05:24 GMT 2024

Total time taken to generate the page: 0.04065 seconds