如果用户进行预订,则会生成与自行车类型一样多的新数据。数据库垂直增长。在进行预订时,它会产生一个唯一的密钥,并且在那一刻它是恒定的。例如: 新的预订。 ' Random0124'和type_1:2数量,type_2:2数量。这意味着type_1 + type_2次存储在数据库中的新行。除自行车类型外,所有数据均相同。
' bicycle_type' => 1个关系数据。这不是数量,这意味着type_1或类似的东西。
基本上我想用预订号码分组。我想返回属于该预订号码的自行车类型和总数。
我想要结合所有内容,因为除了bicycle_type之外,所有数据都是相同的。
我有一个这样的数组:
array:3 [▼
0 => array:12 [▼
"id" => 13
"reservation_number" => "58f382bfc4e52"
"bicycle_type" => 1
"start_date" => "2017-04-23 12:00:00"
"stop_date" => "2017-04-23 13:00:00"
"identity_number" => "11111111110"
"name_lastname" => "John Doe"
"email" => "John@doe.com"
"phone" => "(124) 124 12 41"
"is_complete" => 1
"created_at" => "2017-04-16 17:42:21"
"updated_at" => "2017-04-16 17:42:21"
]
1 => array:12 [▼
"id" => 14
"reservation_number" => "58f382bfc4e52"
"bicycle_type" => 1
"start_date" => "2017-04-23 12:00:00"
"stop_date" => "2017-04-23 13:00:00"
"identity_number" => "11111111110"
"name_lastname" => "John Doe"
"email" => "John@doe.com"
"phone" => "(124) 124 12 41"
"is_complete" => 1
"created_at" => "2017-04-16 17:42:21"
"updated_at" => "2017-04-16 17:42:21"
]
2 => array:12 [▼
"id" => 15
"reservation_number" => "58f382bfc4e52"
"bicycle_type" => 2
"start_date" => "2017-04-23 12:00:00"
"stop_date" => "2017-04-23 13:00:00"
"identity_number" => "11111111110"
"name_lastname" => "John Doe"
"email" => "John@doe.com"
"phone" => "(124) 124 12 41"
"is_complete" => 1
"created_at" => "2017-04-16 17:42:21"
"updated_at" => "2017-04-16 17:42:21"
]
]
我如何组织这样的集合:
array:1 [▼
'58f382bfc4e52' => array:12 [▼
"id" => 13
"reservation_number" => "58f382bfc4e52"
"bicycle_types" => [
'1' => [
'total' => 2,
],
'2' => [
'total' => 1
]
]
"start_date" => "2017-04-23 12:00:00"
"stop_date" => "2017-04-23 13:00:00"
"identity_number" => "11111111110"
"name_lastname" => "John Doe"
"email" => "John@doe.com"
"phone" => "(124) 124 12 41"
"is_complete" => 1
"created_at" => "2017-04-16 17:42:21"
"updated_at" => "2017-04-16 17:42:21"
]
]
答案 0 :(得分:1)
检查循环:
<?php
$as = [
0 => [
"id" => 13,
"reservation_number" => "58f382bfc4e52",
"bicycle_type" => 1,
"start_date" => "2017-04-23 12:00:00",
"stop_date" => "2017-04-23 13:00:00",
"identity_number" => "11111111110",
"name_lastname" => "John Doe",
"email" => "John@doe.com",
"phone" => "(124) 124 12 41",
"is_complete" => 1,
"created_at" => "2017-04-16 17:42:21",
"updated_at" => "2017-04-16 17:42:21",
],
1 => [
"id" => 14,
"reservation_number" => "58f382bfc4e52",
"bicycle_type" => 1,
"start_date" => "2017-04-23 12:00:00",
"stop_date" => "2017-04-23 13:00:00",
"identity_number" => "11111111110",
"name_lastname" => "John Doe",
"email" => "John@doe.com",
"phone" => "(124) 124 12 41",
"is_complete" => 1,
"created_at" => "2017-04-16 17:42:21",
"updated_at" => "2017-04-16 17:42:21"
],
2 => [
"id" => 15,
"reservation_number" => "58f382bfc4e52",
"bicycle_type" => 2,
"start_date" => "2017-04-23 12:00:00",
"stop_date" => "2017-04-23 13:00:00",
"identity_number" => "11111111110",
"name_lastname" => "John Doe",
"email" => "John@doe.com",
"phone" => "(124) 124 12 41",
"is_complete" => 1,
"created_at" => "2017-04-16 17:42:21",
"updated_at" => "2017-04-16 17:42:21",
],
];
$result = [];
foreach ( $as as $a ) {
$reservationNumber = $a['reservation_number'];
// Check if reservation number is exists in the result array or not.
if ( ! isset($result[$reservationNumber]) ) {
// If its not available we assign the current array to result.
$result[$reservationNumber] = $a;
$result[$reservationNumber]['bicycle_type'] = [];
}
// We check if bicycle_type is exists in the result having reservationNumber.
// If its available we increment the value, otherwise initialize it with 1;
if (isset($result[$reservationNumber]['bicycle_type'][$a['bicycle_type']])) {
$result[$reservationNumber]['bicycle_type'][$a['bicycle_type']]['total'] += 1;
} else {
$result[$reservationNumber]['bicycle_type'][$a['bicycle_type']]['total'] = 1;
}
}
echo "<pre>";
print_r($result);
echo "</pre>";
?>