(array)$someemptyvariablethatisnotarray
returns array([0] =>)
instead of array()
How can I make it so I get a empty array that is not iterated when I'm using it inside foreach() ?
The feature which you are using, is called "casting". This means a variable is forced to become a given type, in your example an array. How the var is converted is not always obvious in PHP! In your example $someemptyvariablethatisnotarray becomes an array with one entry with a NULL value. The PHP documentation says:
To solve your code I would recommend something like this:
|
|||||||||
|
Maybe? Though I'm not sure I get the cast, or the purpose. Care to ellaborate a bit more (maybe an example of what you're trying to accomplish?) |
|||||||||
|
how are you? I believe this is what you're after:
You don't get an empty array, because when you set "(array)false", it means you'll have a single element, and that element will have the "FALSE" value assigned to it. Same happens with an empty string (not a null one!) (array)$emptystring will return an array which contains a single element, which is an empty string! Similar to doing:
Hope it helps. Cheers! |
|||
|
When you cast a non-array as an array, it creates an array with that variable as the only value. If you want an empty array, you need to return |
|||
|
var_dump()
instead ofprint_r()
it shows you also the type of the value in the array! – powtac Mar 24 '11 at 15:37