一个数组

时间:2018-03-15 09:41:25

标签: php arrays laravel

情况

我有两个API调用,一个用于获取所有设备,另一个用于获取所有组。我想要完成的是有一个调用将使用其设备获取组。

这是获取设备的功能

public function getAll($environment, $options)
{
    // Get group ids from database, used for pairing groups with environments
    $groups = Device::where('source_id', $options['deviceSourceId'])->get();
    $groupIds = [];
    foreach ($groups as $group) {
        $groupIds[] = $group['group_id'];
    }

    // Parameters
    $apiparams = [
        'headers' => $this->getHeaders($environment)
    ];

    $devices = [];

    foreach($groupIds as $groupId)
    {
        $responseAPI = $this->getClient()->request('GET', 'devices?groupid=' . $groupId, $apiparams);
        $responseAPI = json_decode($responseAPI->getBody()->getContents(), true);
        $devices[] = $responseAPI['devices'];
    }

    return $devices;
}

这是获取群组的功能

public function getGroups($environment, $options)
{
    // Get group ids from database, used for pairing groups with environments
    $groups = Device::where('source_id', $options['deviceSourceId'])->get();
    $groupIds = [];
    foreach ($groups as $group) {
        $groupIds[] = $group['group_id'];
    }

    // Parameters
    $apiparams = [
        'headers' => $this->getHeaders($environment)
    ];

    $groups2 = [];

    foreach($groupIds as $groupId)
    {
        $responseAPI = $this->getClient()->request('GET', 'groups/' . $groupId, $apiparams);
        $responseAPI = json_decode($responseAPI->getBody()->getContents(), true);

        $groups2[] = $responseAPI;
    }
    return $groups2;
}

当前回复

这是我从中得到的回应。

// Device response
[
    {
        "remotecontrol_id": "r5674567"
        "device_id": "d871212",
        "alias": "PC-01",
        "groupid": "g9873491",
        "online_state": "Online",
        "assigned_to": false
    },
    {
        "remotecontrol_id": "r8370129"
        "device_id": "d091231",
        "alias": "PC-02",
        "groupid": "g9873491",
        "online_state": "Offline",
        "assigned_to": false
    }
]

// Group response
{
    "id": "g9873491"
    "name": "companyname",
    "permissions": "owned"
}

需要回复

我想得到的回应是:

[{id: "g9873491",
  name: "companyname",
  devices: [{device_id: "d871212",
             alias: "PC-01",
             online_state: "Online"},
            {device_id: "d091231",
             alias: "PC-02",
             online_state: "Offline"}
            }],
}]

我该怎么做呢?

Sevavietl编辑

public function getAll($environment, $options)
{
    // Get group ids from database, used for pairing groups with environments
    $groups = Device::where('source_id', $options['deviceSourceId'])->get();
    $groupIds = [];
    foreach ($groups as $group) {
        $groupIds[] = $group['group_id'];
    }

    // Parameters
    $apiparams = [
        'headers' => $this->getHeaders($environment)
    ];

    foreach($groupIds as $groupId)
    {

        $responseAPI1 = $this->getClient()->request('GET', 'groups/' . $groupId, $apiparams);
        $responseAPI2 = $this->getClient()->request('GET', 'devices?groupid=' . $groupId, $apiparams);

        $groups2 = json_decode($responseAPI1->getBody()->getContents(), true);
        $devices = json_decode($responseAPI2->getBody()->getContents(), true);

        $result = array_values(array_reduce(
            $devices,
            function ($carry, $device) {
                if (isset($carry[$device['groupid']])) { // Undefined index: groupid
                    // Add device to group.
                    $carry[$device['groupid']]['devices'][] = [
                        'device_id' => $device['device_id'],
                        'alias' => $device['alias'],
                        'online_state' => $device['online_state']
                    ];
                }

                return $carry;
            },
            // Reindex groups by id.
            array_column($groups2, null, 'id')
        ));
    }

    return $result;
}

1 个答案:

答案 0 :(得分:1)

如果您无法控制API,我猜您无法减少调用次数,因为您只有API本身定义的调用。因此,一种解决方案是自己准备所需格式的响应。您可以执行以下操作:

$devices = json_decode($devices, true);
$groups = json_decode($groups, true);

$result = array_values(array_reduce(
    $devices,
    function ($carry, $device) {
        if (isset($carry[$device['groupid']])) {
            // Add device to group.
            $carry[$device['groupid']]['devices'][] = [
                'device_id' => $device['device_id'],
                'alias' => $device['alias'],
                'online_state' => $device['online_state']
            ];
        }

        return $carry;
    },
    // Reindex groups by id.
    array_column($groups, null, 'id')
));

这是the demo

当您使用Laravel时,另一种方法是利用Laravel Collections。但是在这种特殊情况下,我想,除非你是集合大师(并且从封面到封面阅读"Refactoring to Collections" :))普通的PHP数组函数更容易理解和使用。