for循环获取JSON数组中的索引

I have this function to create a device from an API.

    for($i=0;$i < count($response); $i++)
    {
        $device = Device::create([
            'device_id' => $response['devices'][$i]['device_id'],
            'source_id' => Device::getSource($options['deviceSourceId'])->id,
            'alias' => $response['devices'][$i]['alias'],
            'online_state' => $response['devices'][$i]['online_state'],
        ]);
    }

And this is the raw JSON data.

JSON DATA

It needs to get the data from every device from the JSON response and create a device. Can anybody help me out?

Looks like you are working with an array of objects. You could use foreach like so:

// get device source id outside the loop
$deviceSourceId = Device::getSource($options['deviceSourceId'])->id;

foreach($response->devices as $device) {
    // No need to set a variable if you are not using it
    Device::create([
        'device_id' => $device->device_id,
        'source_id' => $deviceSourceId,
        'alias' => $device->alias,
        'online_state' => $device->online_state,
    ]);
}