需求:sql语句中传来的数组格式为data:{内容},需要将其改为
"shop_supplier_id": 10,
"avatarUrl": "https",
"data": [
{内容}
]
并把改变的代码从前台JAVASCRIPT转至后台PHP
JAVASCRIPT:
const result = this.list.reduce((acc, order) => {
// 查找是否已经存在该 status
const group = acc.find(item => item.shop_supplier_id === order.shop_supplier_id);
if (group) {
// 如果存在,推入该 order 到对应的 data 数组
group.data.push(order);
} else {
// 如果不存在,创建一个新组并添加该 order
acc.push({
shop_supplier_id: order.shop_supplier_id,
data: [order]
});
}
return acc;
}, []);
this.list = result;
PHP:
$result1 = array_reduce($result, function ($acc, $order) {
$found = false;
foreach ($acc as &$group) {
if ($group['shop_supplier_id'] === $order['shop_supplier_id']) {
$group['data'][] = $order;
$found = true;
break;
}
}
if (!$found) {
$acc[] = [
'shop_supplier_id' => $order['shop_supplier_id'],
'avatarUrl' => $order['avatarUrl'],
'data' => [$order]
];
}
return $acc;
}, []);
// 返回结果
return $this->renderSuccess('', $result1);
1821

被折叠的 条评论
为什么被折叠?



