Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unset entire array in PHP?

Tags:

arrays

php

I have a loop that runs 47 times on my page. During the course of each loop, any error messages are entered into err[] and are printed out. I'm trying to blank the array after each iteration and I'm running into some trouble.

There could be 4 or 5 error messages per iteration, sometimes none. Is there an easier way of resetting the entire array after each iteration beyond running another foreach loop and unsetting each value? A way of clearing all contents and resetting the indexes without actually removing the array itself?

like image 629
Jeremy DeStefano Avatar asked Nov 01 '11 19:11

Jeremy DeStefano


People also ask

How do you unset an entire array?

Use unset() function to remove array elements in a foreach loop. The unset() function is an inbuilt function in PHP which is used to unset a specified variable.

How do you unset multiple values in PHP?

basically we will unset multiple keys from php array. if you see in php documentation there are not available directly remove multiple keys from php array. But we will create our own php custom function and remove all keys by given array value. In this example i created custom function as array_except().

What does unset do in PHP?

unset() destroys the specified variables. The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy. If a globalized variable is unset() inside of a function, only the local variable is destroyed.

How do you clear a function in PHP?

The reset() function moves the internal pointer to the first element of the array. Related methods: current() - returns the value of the current element in an array. end() - moves the internal pointer to, and outputs, the last element in the array.


2 Answers

You ought to use: unset ( $err );

like image 68
Charles Smith Avatar answered Sep 17 '22 17:09

Charles Smith


Set it to array(), and you should be fine.

like image 32
Ry- Avatar answered Sep 19 '22 17:09

Ry-