14

I have the following array($post_options), it can appear in various guises, the key is dynamic:

It might be like this:

array(1) { [95]=> string(2) "25" }

It might be like this, the key can be anything:

array(1) { [05]=> string(2) "" }

I want to add a control statement that controls upon whether or not the value in the first key element has a value.

I have this code:

if (!isset($post_options[0])) {
            // value is empty
            var_dump($post_options);
        }
        else {
            // value has value
            echo 'Option Selected';
        }

But this is not working, it returns true when the value is set and not set. What is the solution here? Thanks

So if the array appears like this (the value of the first key is empty):

array(1) { [05]=> string(2) "" }

I want to var_dump(); in this case

5
  • can you explain more ? what you want exactly and currently what you getting ? Commented Jan 29, 2016 at 11:38
  • i think try !empty() or empty() condition Commented Jan 29, 2016 at 11:38
  • @jilesh I tried that but no joy, the statement returns true in this case wheter value has value or not. Commented Jan 29, 2016 at 11:43
  • you mean value is empty or not empty in both case right ? Commented Jan 29, 2016 at 11:46
  • What do you mean by "has a value"? Do you mean that the key must exist in the array? Do you mean that the value must not be null? Must the value not be the empty string? How about any other "empty" value? There's so many options here. What precisely do you want? Commented Jan 29, 2016 at 11:53

4 Answers 4

39

Depending on your exact requirements, there are three alternatives.

  1. array_key_exists($key, $array) This is the simplest option. It returns true if the array has the given key, and otherwise returns false.

  2. isset($array[$key]) Slightly more strict than array_key_exists since it also requires that the value assigned to the key is not null.

  3. !empty($array[$key]) (note the !) This is the strictest option, as it requires the value to not be any empty value (i.e., not null, false, 0, '0', etc).

There's some other options, again, depending on exact requirements. It looks to me that you are looking for the !empty option, since it will (in particular) reject empty strings.

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

1 Comment

As a note, assume $var = '' using isset($var) wil return TRUE, to test for and empty string make sure to use empty() - ref: php doc
7

If you don't know the first key of array - use array_keys, for example, to get all keys of your array.

$ar = array('95' => 'val');
$keys = array_keys($ar);
$first_item = $ar[$keys[0]]; 
var_dump($first_item);    // outputs: string(3) "val" 

Another option can be a current function, which will return you current element of an array. Considering that you don't move array pointer, code can be:

$ar = array('95' => 'new_val', '02' => 'val2');
$cur = current($ar);
var_dump($cur);  // outputs: string(7) "new_val" 

After that you can use standard emptyfunction to check:

if (empty($cur))

or

if (empty($first_item))

Comments

1

try this

if (!isset($post_options[0])) {            // value is empty
    if(!empty($post_options[0])) {
          var_dump($post_options);          
    }           
}

1 Comment

empty checks for value and isset checks for array present or not (If you have keys without values then it returns true only. ) To check the value in array we need to use empty
0

isset checks whether the array position exists or not, not whether it cointains or it doesn't contain a value.

You better have a look at the empty function to do this

if (empty($post_options[0])) {
    // value is empty
    var_dump($post_options);
} else {
    // value has value
    echo 'Option Selected';
}

hope this helps

3 Comments

isset actually checks whether the keys exists and does not map to null.
This returns false whether a value is set or not.
then I'm afraid your array element is not empty otherwise the function would work. Try to print the elements in both cases and see the values.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.