1

I have a array what I'm fetching with mysql query per row, each containing the same indexes and it looks as it follows

array(

    0=> array(
         'order_nr'=> 123,
         'order_date' => '2013-01-29 00:00:00',
         'prod_name' => 'prod_1',
         'prod_value' => 1200

       )

    1=> array(
         'order_nr'=> 123,
         'order_date' => '2013-01-29 00:00:00' ,
         'prod_name' => 'prod_2',
         'prod_value' => 2100
    )

)

I would like to merge theme and make the sum of prod_value and creating a new array on prod_name and at the end I would have singe array. How would I do this in php or what would be the best workaround in this case

array(
 'order_nr'=> 123,
 'order_date'=>'2013-01-29 00:00:00',
 'prod_name'=> array('prod_1', 'prod_2')
 'prod_value' => 3300

)
3
  • 5
    At least to me, isn't easy to understand what you're trying to obtain. Please, be more specific and use an "output sample" Commented Feb 5, 2013 at 8:42
  • 1
    if you're trying to sum up these values, I'd suggest to use GROUP BY on the database side, not on the PHP side Commented Feb 5, 2013 at 8:46
  • what is your prod_value ?? Commented Feb 5, 2013 at 8:49

6 Answers 6

1

If the list of products doesn't have to be an array you can use Mysql and aggregating functions. It would be something like this:

SELECT order_no, order_data, GROUP_CONCAT(prod_name SEPARATOR ',') as prod_name, 
    SUM(prod_value) as prod_val FROM Products WHERE ... GROUP BY order_no;

If you need the product names as arrays you can then explode the string:

explode(',', $row['prod_name']);
Sign up to request clarification or add additional context in comments.

3 Comments

and how would I do the concat if I want to select all the columns?
I think this would be the most elegant shortcut
It will also be more efficient than making loops in the code. I don't quite understand the question in the first comment. What do you mean you want select all the columns?
0
$aReturn = array();

foreach( $aData as $entry ) {
 if( empty($aReturn[$entry['order_nr']]) ) {
  $entry['prod_name'] = (array) $entry['prod_name'];
  $aReturn[$entry['order_nr']] = $entry;

  continue;
 }

 $aReturn[$entry['order_nr']]['prod_name'][] = $entry['prod_name'];
 $aReturn[$entry['order_nr']]['prod_value'] += $entry['prod_value'];
}

This will group by order_nr

1 Comment

let me try this seems to be interesting
0
<?php

$arr = array(0=> array(
                         'order_nr'=> 123,
                         'order_date' => '2013-01-29 00:00:00',
                         'prod_name' => 'prod_1',
                         'prod_value' => 1200

                       ),
            1=> array(
                         'order_nr'=> 123,
                         'order_date' => '2013-01-29 00:00:00' ,
                         'prod_name' => 'prod_2',
                         'prod_value' => 2100
                    )

            );

$sigle_arr = array();   
foreach($arr as $val){
   $sigle_arr[$val['order_nr']]['order_nr']     = $val['order_nr'];
   $sigle_arr[$val['order_nr']]['order_date']   = $val['order_date'];
   $sigle_arr[$val['order_nr']]['prod_name'][]  = $val['prod_name'];
   $sigle_arr[$val['order_nr']]['prod_value']  += $val['prod_value'];

}
print_r($sigle_arr);
?>

Comments

0

use array_merge_recursive()

that is the best option for doing it

1 Comment

you can not get 'prod_value' in this case, It needs to be the sum of all the product 'prod_value'
0
$newarray = array();
foreach($array as $v){
 if(!isset($newarray['order_nr'])){ 
 $newarray['order_nr'] = $v['order_nr'];
 }
 if(!isset($newarray['order_date'])){ 
 $newarray['order_date'] = $v['order_date'];
 }
 $newarray['prod_name'][] = $v['prod_name'];

 $newarray['prod_value'] = $newarray['prod_value'] + $v['prod_value'];
}

2 Comments

what if there are two orders in the array, i mean two different 'order_nr'
the question says: same index!
0
$temp = Users::find('all', array('conditions' => array('status' => array('$elemMatch' => array('fields' => array('temp')));
foreach($whichs as $which)
{
  foreach($which['temps'] as $temp)
  {
    if($temp['_id'] == $status['temp'])
    {
    array_push($tempsArray,$temp['status']);
    }
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.