duancan1732 2018-12-19 00:31
浏览 25
已采纳

从另一个数组值创建一个数组

I've been searching for this and can't find it, though it's hard to put in to words so I apologize if it's covered elsewhere.

I have an API call I make that gives me this. This is shifts worked on a given date and there is an array for each shift. This is what one looks like.

$lastMon = array{
       date => $date
       hours => 8
       worker => mjones
       etc
       }

Now I want to take the recruiter value and the hours value and create another array that looks like this

$lastMonData = array{
       key for each $lastMon->recruiter => sum of all $lastMon->hours where recruiter is unique
       total += $lastMon->hours
       }

Basically, I have a bunch of shifts I extract from this system and turn it in to arrays and insert some keys I pull from other parts of the system. I then need an array by date that contains the total hours worked for all the shifts that day, and then a key for each unique name in the recruiter key that adds up the hours mentioned in the same array. I hope this makes sense.

EDIT:

Here is an example

SimpleXMLElement Object
(
    [orderId] => 4000262
    [status] => filled
    [shiftStartTime] => 2018-12-10T07:30:00
    [shiftEndTime] => 2018-12-10T12:30:00
    [tempId] => 80231
    [firstName] => SimpleXMLElement Object
        (
        )

    [lastName] => SimpleXMLElement Object
        (
        )

    [clientId] => 42642
    [clientName] => SimpleXMLElement Object
        (
        )

    [regionName] => SimpleXMLElement Object
        (
        )

    [orderSpecialty] => School
    [orderCertification] => RN
    [floor] => SimpleXMLElement Object
        (
        )

    [shiftNumber] => 1
    [note] => SimpleXMLElement Object
        (
        )

    [payrollNumber] => SimpleXMLElement Object
        (
        )

    [lessLunchMin] => SimpleXMLElement Object
        (
        )

    [dateTimeCreated] => 2018-08-17T09:33:43
    [takenBy] => 592
    [bookedByUserId] => 592
    [orderTypeId] => 1
    [orderType] => SimpleXMLElement Object
        (
        )

    [city] => Holmdel
    [state] => NJ
    [zipCode] => 07733
    [orderSourceID] => SimpleXMLElement Object
        (
        )

    [orderSourceName] => SimpleXMLElement Object
        (
        )

    [lt_orderID] => 13643
    [dateTimeModified] => 2018-08-17T09:33:43
    [recruiter] => John Smith
    [hours] => 05
)
SimpleXMLElement Object
(
    [orderId] => 4002473
    [status] => filled
    [shiftStartTime] => 2018-12-10T09:00:00
    [shiftEndTime] => 2018-12-10T13:30:00
    [tempId] => 1397353
    [firstName] => SimpleXMLElement Object
        (
        )

    [lastName] => SimpleXMLElement Object
        (
        )

    [clientId] => 47597
    [clientName] => SimpleXMLElement Object
        (
        )

    [regionName] => SimpleXMLElement Object
        (
        )

    [orderSpecialty] => School
    [orderCertification] => RN
    [floor] => SimpleXMLElement Object
        (
        )

    [shiftNumber] => 1
    [note] => SimpleXMLElement Object
        (
        )

    [payrollNumber] => SimpleXMLElement Object
        (
        )

    [lessLunchMin] => SimpleXMLElement Object
        (
        )

    [dateTimeCreated] => 2018-08-20T08:53:23
    [takenBy] => 592
    [bookedByUserId] => 592
    [orderTypeId] => 1
    [orderType] => SimpleXMLElement Object
        (
        )

    [city] => Tinton Falls
    [state] => NJ
    [zipCode] => 07701
    [orderSourceID] => SimpleXMLElement Object
        (
        )

    [orderSourceName] => SimpleXMLElement Object
        (
        )

    [lt_orderID] => 0
    [dateTimeModified] => 2018-08-20T10:05:33
    [recruiter] => Mike Jones
    [hours] => 04
)

I need to take the recruiter value and make those in to keys in a new array. Some repeat, so I would only want to take the unique ones. Then, the hours value in this array would be the value for the recruiter name key in the new array.

So this would then look like

$lastMon = array{
      Mike Jones => 4
      John Smith => 5
      Total => 9
}
  • 写回答

1条回答 默认 最新

  • douya1248 2018-12-19 06:18
    关注

    You can use array-reduce to aggregate the recruiter hours:

    $lastMon = array(array("recruiter" => "AAA", "hours" => 4), array("recruiter" => "BBB", "hours" => 5), array("recruiter" => "AAA", "hours" => 3));
    
    function reduceHours($carry, $item)
    {
        if (!array_key_exists($item["recruiter"], $carry))
            $carry[$item["recruiter"]] = 0;
        $carry[$item["recruiter"]] += $item["hours"];
        return $carry;
    }
    
    
    $lastMonData = array_reduce($lastMon, "reduceHours", array());
    

    This will output:

    Array
    (
        [AAA] => 7
        [BBB] => 5
    )
    

    Edited

    Now that you added more data I can give you more detailed answer:

    $str = '<xml><shifts><recruiter>John Smith</recruiter><hours>05</hours></shifts><shifts><recruiter>Mike Jones</recruiter><hours>04</hours></shifts><shifts><recruiter>John Smith</recruiter><hours>08</hours></shifts></xml>';
    $arr = json_decode(json_encode((array) simplexml_load_string($str)), 1);
    
    function sum($carry, $item) { return $carry + $item;}
    function reduceHours($carry, $item)
    {
        if (!array_key_exists($item["recruiter"], $carry))
            $carry[$item["recruiter"]] = 0;
        $carry[$item["recruiter"]] += $item["hours"];
        return $carry;
    }
    
    $lastMonData = array_reduce($arr["shifts"], "reduceHours", array());
    $lastMonData["total"] = array_reduce($lastMonData, "sum");
    

    Converting xml object to array is from here.

    This will give output for $lastMonData:

    Array
    (
        [John Smith] => 13
        [Mike Jones] => 4
        [total] => 17
    )
    

    I know the sum of total can be done in other ways but I tried to keep the example of using array_reduce.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器