'Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes)

I am trying to show categories & subcategories in a tree by using php&pdo This is my code for it

if(! function_exists('fetchCategoryTree')){
    function fetchCategoryTree($status = 1, $spacing = '', $user_tree_array = '') {
        global $con;
        if (!is_array($user_tree_array)) {
            $user_tree_array = array();
        }
    
        $stmt = $con->prepare("SELECT * 
                                FROM `categories` 
                                WHERE `status` = ? 
                                ORDER BY `id` ASC");
        $stmt->execute(array($status));
        $rows = $stmt->fetchAll();
        if ($stmt->rowCount() > 0) {
            foreach ($rows as $row) {

                $user_tree_array[] = array("id" => $row['id'], 
                                            "name" => $spacing . $row['name']);
                $user_tree_array = fetchCategoryTree($row['id'], 
                                $spacing . '  ', $user_tree_array);
            }
        }
        return $user_tree_array;
    }
}

I had tried all the regular solutions like to increase the memory limit to 2048 MB and added this line before the line error code

ini_set('memory_limit','-1');

but it is still the same error show



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source