【问题标题】:Calculate number of elements on the specific criteria根据特定标准计算元素数量
【发布时间】:2018-11-21 12:23:17
【问题描述】:

我有一个像

这样的数组
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); 
}

请帮忙!!!

【问题讨论】:

  • 你的意思是[ RoomId => "9a21e427-16fd-7a9e-bc16-537dc55093bb", NumberOfAllotments => 2 RoomId => "663ghjytf-16fd-7a9e-bc16-537dc55r5g0", NumberOfAllotments => 1 ]还是[ [ RoomId => "9a21e427-16fd-7a9e-bc16-537dc55093bb", NumberOfAllotments => 2], [ RoomId => "663ghjytf-16fd-7a9e-bc16-537dc55r5g0", NumberOfAllotments => 1] ]
  • 我想获得每个房间完成的分配数量,这些分配数量存储在上述问题的数组中。像房间 id "9a21e427-16fd-7a9e-bc16-537dc55093bb" 有两个分配 id 像 "6b594c22-73f1-0a2a-298c-2d3feba2905d", "99730f9a-2242-24bd-0908-b9035a75d328" 这样的数字roomid 并将它们放在单独的数组中

标签: php php-5.3 php-5.6


【解决方案1】:

我认为你不需要循环。
使用 array_column 和 array_count_values。

$Roomid = array_count_values(array_column($arr, "RoomId"));

现在 Roomid 键是 Roomid,值是每个的计数。

如果你必须有你的问题中的数组,你可以这样做:

$Roomid = array_count_values(array_column($arr, "RoomId"));
Foreach($Roomid as $room => $count){
    $arr[] = ["Roomid" => $room, 'NumberOfAllotments' => $count];
}

这只会循环每个唯一的房间,而不是整个数组。

输出:
https://3v4l.org/ohUJS

【讨论】:

    【解决方案2】:

    试试这个:

    // 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/ 我添加了一个带有 roomId 和分配数量的新数组

    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 数组函数找到另一种方法,但是这种方法有效(如果我理解您想要什么!)

    【讨论】:

    • 我想获得每个房间完成的分配数量,这些分配数量存储在上述问题的数组中。像房间 id "9a21e427-16fd-7a9e-bc16-537dc55093bb" 有两个分配 id 像 "6b594c22-73f1-0a2a-298c-2d3feba2905d"、"99730f9a-2242-24bd-0908-b9035a75d328" 这样的数字将是房间里
    • @NidaAmin 是的,我没有这样做?我为每个 RoomId 和这个 roomId 的分配数量构建了一个数组,问题出在哪里?
    【解决方案3】:

    试试这个代码:

    首先计算并保存在一个名为 $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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-14
      • 1970-01-01
      • 2023-01-13
      • 1970-01-01
      • 2017-07-29
      • 2022-10-04
      • 2013-10-04
      • 1970-01-01
      相关资源
      最近更新 更多