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 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题