根据特定条件计算元素数
Calculate number of elements on the specific criteria
我有一个类似
的数组
array(3) {
[0]=> array(2) {
["AllotmentId"]=> string(36) "6b594c22-73f1-0a2a-298c-2d3feba2905d"
["RoomId"]=> string(36) "9a21e427-16fd-7a9e-bc16-537dc55093bb" }
[1]=> array(2) {
["AllotmentId"]=> string(36) "99730f9a-2242-24bd-0908-b9035a75d328"
["RoomId"]=> string(36) "9a21e427-16fd-7a9e-bc16-537dc55093bb" }
[2]=> array(2) {
["AllotmentId"]=> string(36) "fft54c22-73f1-0a2a-2558c-2d3fgged"
["RoomId"]=> string(36) "663ghjytf-16fd-7a9e-bc16-537dc55r5g0" }
}
现在我想创建另一个数组来存储以下信息,例如
[
RoomId => "9a21e427-16fd-7a9e-bc16-537dc55093bb", NumberOfAllotments => 2
RoomId => "663ghjytf-16fd-7a9e-bc16-537dc55r5g0", NumberOfAllotments => 1
]
它应该根据房间 ID 计算分配 ID 的数量并创建另一个数组。
我无法调整以下代码行。
foreach ($allotmentArray as $key => $allotment)
{
$arrayToPush = array('RoomId' => $allotment["RoomId"], "NumberOfAllotments" => $count]);
array_push($freeRooms, $arrayToPush);
}
请帮忙!!!
试试这个代码:
首先统计并保存在一个名为$targetArray的键值数组中。
然后在 $outputArray 中生成您喜欢的输出:
<?php
$targetArray = array();
foreach ($allotmentArray as $allotment)
{
if(!array_key_exists($allotment['RoomId'], $targetArray))
$targetArray[$allotment['RoomId']] = 0;
$targetArray[$allotment['RoomId']]++;
}
$outputArray = array();
foreach($targetArray as $RoomId=> $count)
$outputArray[] = array('RoomId'=>,'count'=>$count);
var_dump($outputArray);
试试这个:
// Your array
$array = array(
0 => array(
"AllotmentId" => "6b594c22-73f1-0a2a-298c-2d3feba2905d",
"RoomId" => "9a21e427-16fd-7a9e-bc16-537dc55093bb" ),
1=> array(
"AllotmentId"=> "99730f9a-2242-24bd-0908-b9035a75d328",
"RoomId" => "9a21e427-16fd-7a9e-bc16-537dc55093bb" ),
2=> array(
"AllotmentId"=> "fft54c22-73f1-0a2a-2558c-2d3fgged",
"RoomId"=> "663ghjytf-16fd-7a9e-bc16-537dc55r5g0" )
);
1/ 我创建一个临时数组
$temp_array = array();
2/ 然后我在这个临时数组中添加所有 roomId 值
foreach ($array as $key => $data) {
$temp_array[] = $data['RoomId'];
}
3/现在我统计每个roomId的个数
$temp_result = array_count_values($temp_array);
这里的输出是:
array (size=2)
'9a21e427-16fd-7a9e-bc16-537dc55093bb' => int 2
'663ghjytf-16fd-7a9e-bc16-537dc55r5g0' => int 1
4/ 现在我将构建我的结果数组
$result = array();
5/ 我添加了一个包含房间 ID 和分配数量的新数组
foreach ($temp_result as $roomId => $NumberOfAllotments) {
$result[] = array(
'RoomId' => $roomId,
'NumberOfAllotments' => $NumberOfAllotments
);
}
这是输出:
array (size=2)
0 =>
array (size=2)
'RoomId' => string '9a21e427-16fd-7a9e-bc16-537dc55093bb' (length=36)
'NumberOfAllotments' => int 2
1 =>
array (size=2)
'RoomId' => string '663ghjytf-16fd-7a9e-bc16-537dc55r5g0' (length=36)
'NumberOfAllotments' => int 1
是您要找的吗?
我认为您可以使用一些 php 数组函数找到其他方法,但此方法有效(如果我理解您想要的!)
据我所知,您不需要循环。
使用 array_column 和 array_count_values。
$Roomid = array_count_values(array_column($arr, "RoomId"));
现在Roomid键就是Roomid,值就是每个Roomid的个数。
如果您必须拥有问题中的数组,您可以这样做:
$Roomid = array_count_values(array_column($arr, "RoomId"));
Foreach($Roomid as $room => $count){
$arr[] = ["Roomid" => $room, 'NumberOfAllotments' => $count];
}
这只会循环每个独特的房间,而不是整个数组。
我有一个类似
的数组array(3) {
[0]=> array(2) {
["AllotmentId"]=> string(36) "6b594c22-73f1-0a2a-298c-2d3feba2905d"
["RoomId"]=> string(36) "9a21e427-16fd-7a9e-bc16-537dc55093bb" }
[1]=> array(2) {
["AllotmentId"]=> string(36) "99730f9a-2242-24bd-0908-b9035a75d328"
["RoomId"]=> string(36) "9a21e427-16fd-7a9e-bc16-537dc55093bb" }
[2]=> array(2) {
["AllotmentId"]=> string(36) "fft54c22-73f1-0a2a-2558c-2d3fgged"
["RoomId"]=> string(36) "663ghjytf-16fd-7a9e-bc16-537dc55r5g0" }
}
现在我想创建另一个数组来存储以下信息,例如
[
RoomId => "9a21e427-16fd-7a9e-bc16-537dc55093bb", NumberOfAllotments => 2
RoomId => "663ghjytf-16fd-7a9e-bc16-537dc55r5g0", NumberOfAllotments => 1
]
它应该根据房间 ID 计算分配 ID 的数量并创建另一个数组。
我无法调整以下代码行。
foreach ($allotmentArray as $key => $allotment)
{
$arrayToPush = array('RoomId' => $allotment["RoomId"], "NumberOfAllotments" => $count]);
array_push($freeRooms, $arrayToPush);
}
请帮忙!!!
试试这个代码:
首先统计并保存在一个名为$targetArray的键值数组中。 然后在 $outputArray 中生成您喜欢的输出:
<?php
$targetArray = array();
foreach ($allotmentArray as $allotment)
{
if(!array_key_exists($allotment['RoomId'], $targetArray))
$targetArray[$allotment['RoomId']] = 0;
$targetArray[$allotment['RoomId']]++;
}
$outputArray = array();
foreach($targetArray as $RoomId=> $count)
$outputArray[] = array('RoomId'=>,'count'=>$count);
var_dump($outputArray);
试试这个:
// Your array
$array = array(
0 => array(
"AllotmentId" => "6b594c22-73f1-0a2a-298c-2d3feba2905d",
"RoomId" => "9a21e427-16fd-7a9e-bc16-537dc55093bb" ),
1=> array(
"AllotmentId"=> "99730f9a-2242-24bd-0908-b9035a75d328",
"RoomId" => "9a21e427-16fd-7a9e-bc16-537dc55093bb" ),
2=> array(
"AllotmentId"=> "fft54c22-73f1-0a2a-2558c-2d3fgged",
"RoomId"=> "663ghjytf-16fd-7a9e-bc16-537dc55r5g0" )
);
1/ 我创建一个临时数组
$temp_array = array();
2/ 然后我在这个临时数组中添加所有 roomId 值
foreach ($array as $key => $data) {
$temp_array[] = $data['RoomId'];
}
3/现在我统计每个roomId的个数
$temp_result = array_count_values($temp_array);
这里的输出是:
array (size=2)
'9a21e427-16fd-7a9e-bc16-537dc55093bb' => int 2
'663ghjytf-16fd-7a9e-bc16-537dc55r5g0' => int 1
4/ 现在我将构建我的结果数组
$result = array();
5/ 我添加了一个包含房间 ID 和分配数量的新数组
foreach ($temp_result as $roomId => $NumberOfAllotments) {
$result[] = array(
'RoomId' => $roomId,
'NumberOfAllotments' => $NumberOfAllotments
);
}
这是输出:
array (size=2)
0 =>
array (size=2)
'RoomId' => string '9a21e427-16fd-7a9e-bc16-537dc55093bb' (length=36)
'NumberOfAllotments' => int 2
1 =>
array (size=2)
'RoomId' => string '663ghjytf-16fd-7a9e-bc16-537dc55r5g0' (length=36)
'NumberOfAllotments' => int 1
是您要找的吗?
我认为您可以使用一些 php 数组函数找到其他方法,但此方法有效(如果我理解您想要的!)
据我所知,您不需要循环。
使用 array_column 和 array_count_values。
$Roomid = array_count_values(array_column($arr, "RoomId"));
现在Roomid键就是Roomid,值就是每个Roomid的个数。
如果您必须拥有问题中的数组,您可以这样做:
$Roomid = array_count_values(array_column($arr, "RoomId"));
Foreach($Roomid as $room => $count){
$arr[] = ["Roomid" => $room, 'NumberOfAllotments' => $count];
}
这只会循环每个独特的房间,而不是整个数组。