up vote 0 down vote favorite

how can i check if logo exists in this array called $attachements print_r is below:

Array ( [logo] => /home/richar2/public_html/ioagh/images/stories/jreviews/20100510115659_1_img.gif )

when theres no logo, the array print_r's

Array ( )

i tried: if (isset($attachments['logo']) ) {..} but the conditional code runs when there is no logo

flag

5 Answers

up vote 1 down vote

It's very stange that isset() is not working, I am pretty sure it should. Maybe you have a problem elsewhere in your code.

Anyway, if you want to try something else, there is a specific function: array_key_exists()

link|flag
up vote 0 down vote

Use the function array_key_exists.

http://php.net/manual/en/function.array-key-exists.php

link|flag
up vote 0 down vote

but the conditional code runs when there is no logo

You could construct an else clause to take appropriate action:

if (isset($attachments['logo']))
{
  // logo is set
}
else
{
  // loto is not set
}

Or simply try this:

if (array_key_exists('logo', $attachments))
{
    // logo is set
}

More info on array_key_exists

link|flag
up vote 0 down vote

You can use array_key_exists.

link|flag
up vote 0 down vote

This works for me as expected:

$arr['logo'] = '/home/richar2/public_html/ioagh/images/stories/jreviews/20100510115659_1_img.gif';
print_r($arr);

if (isset($arr['logo'])){
    echo $arr['logo'];
}else{
    echo 'Key doesn\'t exist!';
}

Are you sure you set $arr['logo'] = null, not $arr['logo'] = ''? For the latter you can also check

if (isset($arr['logo'] && !empty($arr['logo'])){
...
}
link|flag
If that's the case ($arr['logo'] = ''), you should use empty() to check if $arr['logo'] is set to a non-null value. – MartinodF May 10 at 11:14

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.