从最后一个小时挣扎到格式化这个阵列,我没有成功,这里是我的阵列,它有重复的客户ID但不同的佣金ID。下面我写了我想要的格式。
这是我的代码。
$coll = Mage::getModel('commission/commission')->getCollection();
$fn = Mage::getModel('eav/entity_attribute')->loadByCode('1', 'firstname');
$ln = Mage::getModel('eav/entity_attribute')->loadByCode('1', 'lastname');
$coll->getSelect()
->join(array('ce1' => 'customer_entity_varchar'), 'ce1.entity_id=main_table.customer_id', array('firstname' => 'value'))
->where('ce1.attribute_id='.$fn->getAttributeId())
->join(array('ce2' => 'customer_entity_varchar'), 'ce2.entity_id=main_table.customer_id', array('lastname' => 'value'))
->where('ce2.attribute_id='.$ln->getAttributeId())
->columns(new Zend_Db_Expr("CONCAT(`ce1`.`value`, ' ',`ce2`.`value`) AS fullname"));
$values = $this->formatData($coll->getData());
It will return the Following array.
Array
(
[0] => Array
(
[commission_id] => 1
[customer_id] => 42
[order_id] => 116
[store_id] => 1
[website_id] => 1
[transaction_id] => 8
[price] => 5
[created_at] => 2014-07-10 07:54:19
[firstname] => sam
[lastname] => sundar
[fullname] => sam sundar
)
[1] => Array
(
[commission_id] => 2
[customer_id] => 42
[order_id] => 116
[store_id] => 1
[website_id] => 1
[transaction_id] => 8
[price] => 5
[created_at] => 2014-07-10 07:54:19
[firstname] => sam
[lastname] => sundar
[fullname] => sam sundar
)
[2] => Array
(
[commission_id] => 3
[customer_id] => 41
[order_id] => 110
[store_id] => 1
[website_id] => 1
[transaction_id] => 2
[price] => 5
[created_at] => 2014-07-01 00:00:00
[firstname] => king
[lastname] => street
[fullname] => king street
)
[3] => Array
(
[commission_id] => 4
[customer_id] => 41
[order_id] => 110
[store_id] => 1
[website_id] => 1
[transaction_id] => 2
[price] => 5
[created_at] => 2014-07-01 00:00:00
[firstname] => king
[lastname] => street
[fullname] => king street
)
[4] => Array
(
[commission_id] => 5
[customer_id] => 42
[order_id] => 117
[store_id] => 1
[website_id] => 1
[transaction_id] => 3
[price] => 5
[created_at] => 2014-07-10 08:09:22
[firstname] => sam
[lastname] => sundar
[fullname] => sam sundar
)
[5] => Array
(
[commission_id] => 6
[customer_id] => 42
[order_id] => 118
[store_id] => 1
[website_id] => 1
[transaction_id] => 5
[price] => 5
[created_at] => 2014-07-10 08:21:24
[firstname] => sam
[lastname] => sundar
[fullname] => sam sundar
)
[6] => Array
(
[commission_id] => 7
[customer_id] => 44
[order_id] => 119
[store_id] => 13
[website_id] => 4
[transaction_id] => 63
[price] => 53
[created_at] => 2014-07-11 06:37:40
[firstname] => savi
[lastname] => suma
[fullname] => savi suma
)
) ;
我想要以下格式。
Array(
0=>array(
'customer_id' => 42
'fullname' => 'sam sundar'
'totalPrice' => 20
)
1=>array(
'customer_id' => 44
'fullname' => 'savi suma'
'totalPrice' => 53
)
1=>array(
'customer_id' => 41
'fullname' => 'king street'
'totalPrice' => 10
)
);
提前致谢
Thank You so much guys.
Here is My answer to my question, finaly i figured it out.
public function formatData($data)
{
$temp = array();
foreach($data as $value)
{
$temp[$value['customer_id']]['fees'][] = $value['foundation_fee'];
$temp[$value['customer_id']]['created_at'][] = $value['created_at'];
$temp[$value['customer_id']]['fullName'][] = $value['fullname'];
}
$output = array();
foreach($temp as $key => $labels)
{
$output[] = array(
'customer_id' => $key,
'fees' => $labels['fees'],
'created_at'=>$labels['created_at'],
'fullName'=>$labels['fullName'],
);
}
foreach ($output as $key => $value) {
$result[$key]['customer_id'] = $value['customer_id'];
$result[$key]['fullName'] = array_unique($value['fullname']);
$result[$key]['totalFees'] += array_sum($value['fees']);
}
return $result;
}
答案 0 :(得分:0)
试试这个:
foreach ($data as $item) {
$new_array[] = array("customer_id" => $item['customer_id'], "fullname" => $item['fullname'],"totalPrice"=>$item['totalPrice'] );
}
print_r($new_array);
$data
是你的数组。
答案 1 :(得分:0)
这是一个简单数组中“嵌套组”的示例。要求是按'customer_id'进行分组,并将每个组的价格合计。
如果'customer_id'的所有记录在输入列表中都是“在一起”,则会更容易处理。这将允许我们按顺序处理记录,检查'customer_id'的变化。
假设初始数组是从“SQL查询”获得的,那么如果查询按'customer_id'返回行顺序,则会更容易。这将消除“排序”步骤。
算法:
1)按'customer_id'排序数组。
2)使用“预读”技术汇总每个'customer_id'组以轻松处理该组。它确实意味着我们不能使用'foreach'循环,因为我们需要阅读'group'处理中的下一条记录。
代码: Demonstration of the the full code at 'viper-7'
输入数组我称之为'$ commission',它是问题中提供的测试数据。
调用代码和输出:
// sort commission
$sortedCommission = sortByCustomerId($commission);
// sum commission
$sumCustomers = sumCustomerGroups($sortedCommission);
// display input and output.
echo '<pre>';
var_dump($sortedCommission);
var_dump($sumCustomers);
echo '</pre>';
exit;
对已排序的'委托'数组求和的函数:
// use 'read ahead' so that we always have a row to test against
function sumCustomerGroups($commission)
{
$sumCustomerIdList = array();
$iterCustomerId = new ArrayIterator($commission);
$curCustomer = $iterCustomerId->current(); // read first record
while ($iterCustomerId->valid()) { // process until end
$curCustomerId = $curCustomer['customer_id']; // current customer
$sumCustomer = array('customer_id' => $curCustomer['customer_id'],
'fullname' => $curCustomer['fullname'],
'totalPrice' => 0
);
while ($iterCustomerId->valid() && $curCustomer['customer_id'] === $curCustomerId) {
$sumCustomer['totalPrice'] += $curCustomer['price'];
$iterCustomerId->next(); // read next record
$curCustomer = $iterCustomerId->current();
}
// add to the output array
$sumCustomerIdList[] = $sumCustomer;
}
return $sumCustomerIdList;
}
对'委托'数组进行排序的功能
// sort the commission array by 'customer_id'.
function sortByCustomerId($source)
{
usort($source, function ($e1, $e2) {
if ($e1['customer_id'] < $e2['customer_id']) {
return -1;
}
elseif ($e1['customer_id'] > $e2['customer_id']) {
return +1;
}
else {
return 0;
}
});
return $source;
}