我有一个如下所示的数组
[0] = Array
(
[title] => Khamna,
[booking_number] = 200003852,
[quantity] =1,
[unit_price] = 5.00,
[gross_total] = 5.00,
)
[1] = Array
(
[title] = Khamna
[booking_number] = 200003857
[quantity] = 2
[unit_price] = 5.00
[gross_total] = 410.00
)
[2] = Array
(
[title] = Khamna
[booking_number] = 200003857
[quantity] = 2
[unit_price] = 200.00
[gross_total] = 410.00
)
[3] = Array
(
[title] = Khamna
[booking_number] = 200003858
[quantity] = 1
[unit_price] = 200.00
[gross_total] = 200.00
)
我想结果输出如booking_number将是数组的关键,子数组将基于“unit_price
”和“quantity
”..
unit_price和数量为子阵列超过1或2或3
[200003852] => Array
(
[title] =Khamna
[gross_total] = 5.00
[detail] = Array
( 0 => array([quantity] = 1
[unit_price] = 5.00),
)
)
[200003857] = Array
(
[title] = Khamna
[gross_total] = 410.00,
[detail] = Array
( 0 => array([quantity] = 2
[unit_price] = 5.00),
1 => array([quantity] = 2
[unit_price] = 200.00),
)
)
[200003857] = Array
(
[title] = Khamna
[gross_total] = 10.00
[detail] = Array
( 0 => array([quantity] = 2
[unit_price] = 5.00),
)
)
答案 0 :(得分:2)
这会对你有帮助......
$result = array();
foreach($array as $row) {
if(!isset($result[$row['booking_number']])) {
$result[$row['booking_number']] = array(
'title' => $row['title'],
'gross_total' => $row['gross_total']
);
}
$result[$row['booking_number']]['details'][] = array(
'quantity' => $row['quantity'],
'unit_price' => $row['unit_price']
);
}
答案 1 :(得分:0)
此代码可以帮助您。
$res = [];
foreach($array as $v){
$res[$v['booking_number']] =
[
'title'=>$v['title'],
'gross_total'=>$v['gross_total'],
'detail'=>[['quantity'=>$v['quantity']],
['unit_price'=>$v['unit_price']]]
];
}
echo '<pre>'; print_r($res);
答案 2 :(得分:0)
在你的orignal数组之后运行它
foreach ($arra as $key) {
$arr[$key['booking_number']]=array('title'=> $key['title'],'gross_total'
=>$key['gross_total'],'detail'=>array('0' =>
$key['unit_price'],'1' => $key['quantity']) );
}
注意:$ arra是你的原始数组
它会使用
打印您的数组echo "<pre>";
print_r($arr);
Array
(
[200003852] => Array
(
[title] => Khamna
[gross_total] => 5
[detail] => Array
(
[0] => 5
[1] => 1
)
)
[200003857] => Array
(
[title] => Khamna
[gross_total] => 410
[detail] => Array
(
[0] => 5
[1] => 2
)
)
)