All the 12 boys and their football coach have been rescued from a flooded cave in northern Thailand after being trapped for 18 days, bringing an end to the ordeal that prompted a huge international rescue effort. 

The coach was one of the last to be extricated from the cave on Tuesday in the rescue operation that began as a local search for the missing but turned into a complex mission, involving hundreds of experts who flew in from around the world to help, media reported. 

"The 12 Wild Boars (name of their football club) and coach have emerged from the cave and they are safe," the Thai Navy Seal unit said on its official Facebook page. It added: "Hooyah." 

The final day of the operation began just after 10 a.m. on Tuesday as the first eight boys, freed in operations on Sunday and Monday, recuperated at a hospital in nearest Chiang Rai city. 

The 12 young footballers, aged between 11 and 16, and their 25-year-old coach got trapped on June 23 while exploring the Tham Luang cave in northern Thailand's Chiang Rai province after soccer practice and a rainy season downpour flooded the tunnel. 

The boys were found inside the cave by British rescue divers a week ago, about 4 km from the cave mouth.  

Thirteen foreign cave diving experts and five Thai Navy rescuers joined the first operation on Sunday that brought out four boys from the cave. Rescuers spent about 11 hours on the mission. Another four boys were brought out by the same team on Monday. 

The remaining four boys and their coach were rescued on Tuesday and were sent to hospital by helicopters. They underwent X-rays and blood tests and will remain under observation in the hospital for at least seven days. 

The news was greeted by global jubilation and the rescue workers were lauded. US President Donald Trump tweeted "great job" and British Prime Minister Theresa May said: "The world was watching and will be saluting the bravery of all those involved." 

Overnight, entrepreneur Elon Musk posted on social media that he had personally delivered a child-sized submarine to the site which he had developed to assist with the operation, but it was unlikely to play a role. 

In response, Narongsak Osatanakorn, the head of the joint command centre coordinating the operation, said: "Although his technology is good and sophisticated, it's not practical for this mission."