12

(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() ?

1
  • 3
    When you use var_dump() instead of print_r() it shows you also the type of the value in the array! Commented Mar 24, 2011 at 15:37

8 Answers 8

25
$var = array();

will empty an array. Is this what you're after?

Sign up to request clarification or add additional context in comments.

Comments

19

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:

The behaviour of an automatic conversion to array is currently undefined.

To solve your code I would recommend something like this:

if (!is_array($someemptyvariablethatisnotarray) {
    $someemptyvariablethatisnotarray = array();
}

2 Comments

If you casted NULL as an array, it would become a blank array, if you cast a scalar value (even an empty string, because that's not NULL) it becomes an array with that value, not a NULL value.
4
if(!$variable){
        return array();
}

Comments

3
$somevar = empty($somevar) ? array() : (array)$somevar;

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?)

4 Comments

I'm using it like foreach((array)$something as $k).... Sometimes $something may be "false" and not a array, so I don't want foreach to iterate it
@Alexandra: I know PHP's flexible, but if you're finding your variables switching datatypes on you, there should be a check somewhere else to catch that (don't rely on a cast in other words). I'm going to jump the gun and assume it's database related, and a fetch row which could potentially return a FALSE which would inhibit you iterating over the elements, correct?
yes :) I hate making checks for every small thing, but I guess I have no choice here..
@Alexandra: I might advise while (($row = mysql_fetch_array($result)) !== false){ ... }. That's still a "check". ;-)
3

Try unset($someemptyvariablethatisnotarray[0]) :)

Comments

3

how are you?

I believe this is what you're after:

$something = false;
foreach((array)(empty($something) ? null : $something) as $k){
    echo 'never enters here';
}

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:

array('');

Hope it helps.

Cheers!

Comments

2

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 array().

Comments

0

just use count(), for example: if(count($array) == 0 ){ // empty array }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.