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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里