Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this multi dimensional array in the form of tree.

$menu = array('records'=>array(
                        array('id'=>1,
                              'parent_id'=>NULL,
                              'name'=>'Food',
                              'type'=>'category',
                              'records'=>array(
                                                  array('id'=>2,
                                                      'parent_id'=>1,
                                                      'name'=>'Breakfast',
                                                      'type'=>'category',
                                                      'records'=>array(
                                                                          array('id'=>3,
                                                                              'parent_id'=>2,
                                                                              'name'=>'Omelette',
                                                                              'type'=>'category',
                                                                              'records'=>array(
                                                                                              array('id'=>4,
                                                                                                  'parent_id'=>3,
                                                                                                  'name'=>'Turkish',
                                                                                                  'type'=>'item'),
                                                                                            array('id'=>5,
                                                                                                  'parent_id'=>3,
                                                                                                  'name'=>'Indian',
                                                                                                  'type'=>'item'),
                                                                                            array('id'=>6,
                                                                                                  'parent_id'=>1,
                                                                                                  'name'=>'American',
                                                                                                  'type'=>'item')
                                                                                          ),
                                                                              'statistics'=>array('sale_qty'=>0,
                                                                                                    'gross_sale'=>0,
                                                                                                  'net_sale'=>0,
                                                                                                  'comp_qty'=>0,
                                                                                                  'comp_total'=>0)
                                                                        ),
                                                                        array('id'=>7,
                                                                              'parent_id'=>1,
                                                                              'name'=>'Bread',
                                                                              'type'=>'item')
                                                                  ),
                                                      'statistics'=>array('sale_qty'=>0,
                                                                          'gross_sale'=>0,
                                                                          'net_sale'=>0,
                                                                          'comp_qty'=>0,
                                                                          'comp_total'=>0)
                                                      ),
                                                array('id'=>8,
                                                      'parent_id'=>1,
                                                      'name'=>'Jam',
                                                      'type'=>'item')
                                        ),
                              'statistics'=>array('sale_qty'=>0,
                                                    'gross_sale'=>0,
                                                  'net_sale'=>0,
                                                  'comp_total'=>0)
                              )
                      ),
         'statistics'=>array('sale_qty'=>0,
                             'gross_sale'=>0,
                            'net_sale'=>0,
                            'comp_qty'=>0,
                            'comp_total'=>0)
        );

Then i have this another array of log

$log = array('ids'=>array(5,8),
               5=>array('id'=>5,
                         'parent_id'=>3,
                         'name'=>'Indian',
                         'type'=>'item',
                         'item_price'=>'475.00',
                         'item_tax'=>'76.000000',
                         'sale_count'=>'1',
                         'gross_total'=>551,
                         'complimentry_count'=>'0',
                         'complimentry_total'=>0,
                         'complimentry_net_total'=>0,
                         'net_total'=>475),
               8=>array('id'=>8,
                         'parent_id'=>1,
                         'name'=>'Jam',
                         'type'=>'item',
                         'item_price'=>'603.45',
                         'item_tax'=>'"96.55',
                         'sale_count'=>'1',
                         'gross_total'=>700,
                         'complimentry_count'=>'0',
                         'complimentry_total'=>0,
                         'complimentry_net_total'=>0,
                         'net_total'=>603.45)
    );

What i am trying to do here is to search the values which are in $log['ids'], in the $menu array by matching the 'id' key. If the value is found in $menu then replace it with the appropriate array found in $log.

For example find value 5 in $menu if found replace it with $log[5].

After this is done rest of the nodes which are not needed and are not in the path would be removed and the sum of sale_count etc. of each leaf (item) would be equal to the value of that leaves immediate parent statistics node.

Final array would something like this

$finalmenu = array('records'=>array(
                        array('id'=>1,
                              'parent_id'=>NULL,
                              'name'=>'Food',
                              'type'=>'category',
                              'records'=>array(
                                                  array('id'=>2,
                                                      'parent_id'=>1,
                                                      'name'=>'Breakfast',
                                                      'type'=>'category',
                                                      'records'=>array(
                                                                          array('id'=>3,
                                                                              'parent_id'=>2,
                                                                              'name'=>'Omelette',
                                                                              'type'=>'category',
                                                                              'records'=>array(
                                                                                            array('id'=>5,
                                                                                                  'parent_id'=>3,
                                                                                                  'name'=>'Indian',
                                                                                                  'type'=>'item',
                                                                                                  'item_price'=>'475.00',
                                                                                                  'item_tax'=>'76.000000',
                                                                                                  'sale_count'=>'1',
                                                                                                  'gross_total'=>551,
                                                                                                  'complimentry_count'=>'0',
                                                                                                  'complimentry_total'=>0,
                                                                                                  'complimentry_net_total'=>0,
                                                                                                  'net_total'=>475)
                                                                                          ),
                                                                              'statistics'=>array('sale_qty'=>1,
                                                                                                  'gross_sale'=>551,
                                                                                                  'net_sale'=>475,
                                                                                                  'comp_qty'=>0,
                                                                                                  'comp_total'=>0)
                                                                        ),
                                                                  ),
                                                      'statistics'=>array('sale_qty'=>1,
                                                                          'gross_sale'=>551,
                                                                          'net_sale'=>475,
                                                                          'comp_qty'=>0,
                                                                          'comp_total'=>0)
                                                      ),
                                                array('id'=>8,
                                                      'parent_id'=>1,
                                                      'name'=>'Jam',
                                                      'type'=>'item',
                                                      'item_price'=>'603.45',
                                                      'item_tax'=>'96.55',
                                                      'sale_count'=>'1',
                                                      'gross_total'=>700,
                                                      'complimentry_count'=>'0',
                                                      'complimentry_total'=>0,
                                                      'complimentry_net_total'=>0,
                                                      'net_total'=>603.45)
                                        ),
                              'statistics'=>array('sale_qty'=>2,
                                                  'gross_sale'=>1251,
                                                  'net_sale'=>1078.45,
                                                  'comp_total'=>0)
                              )
                      ),
         'statistics'=>array('sale_qty'=>2,
                             'gross_sale'=>1251,
                             'net_sale'=>1078.45,
                             'comp_total'=>0)
        ); 

I have been trying to get this done for around a week but i've only been able to get to the replacement part but not after that. I have tried RecursiveIteratorIterator with RecursiveArrayIterator but no avail.

If anyone has answer to this it would be hugely appreciated.

Thanks.

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.