php - Adding Custom WordPress Rest API

I have been working on custom WordPress rest API Endpoint. Goal is to create a WordPress custom route in this route i want to get Category Id and convert it to Category Name. I have written the function but its returning null for the category Id. The function simply get the categories of the WordPress and register the route.How Can I get the all Categories Id and convert category Id into category name.

    function w_categories()
{

    $categories = get_categories();

    $data = [];
    $i = 0;

    foreach ($categories as $category) {

        $data[$i]['id'] = $category->ID;
        $i++;
    }

    return $data;
}
add_action('rest_api_init', function () {
    register_rest_route('w/v2', 'trending', [
        'methods' => 'GET',
        'callback' => 'w_categories',
    ]);
});

Answer

Solution:

After Changing the $category->ID to $category->cat_ID. Solve the problem.

Source