简体   繁体   中英

php array operations with indexes

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

)

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

<?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);
?>

use array_merge_recursive()

that is the best option for doing it

$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'];
}
$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']);
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM