views:

397

answers:

5

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

A: 

Use the function array_key_exists.

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

MasterGaurav
+1  A: 

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

MartinodF
A: 

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

Sarfraz
A: 

You can use array_key_exists.

unholysampler
A: 

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'])){
...
}
Indrek
If that's the case ($arr['logo'] = ''), you should use empty() to check if $arr['logo'] is set to a non-null value.
MartinodF