Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way to empty an array

Tags:

php

I have an array containing several keys, values, objects etc.. I need to empty that array but I'd like to do it in the most efficient manner.

The best I can come up with is:

foreach ($array as $key => $val) unset($array[$key]);

But I don't like the idea of having to loop through the array to just empty it.. surely there's a nice slick/clever way of doing this without wasting memory creating a new array?

Note: I'm not sure myself if it does cost extra memory in creating the array as new again. If it doesn't then $array = new array(); would be a fine way of 'emptying' it.

like image 751
John Hunt Avatar asked Jan 18 '13 12:01

John Hunt


1 Answers

Just try with:

$array = array();
like image 57
hsz Avatar answered Oct 04 '22 19:10

hsz