Forum Moderators: coopster

Message Too Old, No Replies

How to check if an array element has a value?

         

irock

6:12 am on Oct 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi, whenever I tried to use a simple IF statement (if $items[0]['store-offer_store-name'] == "") to check if the variable has a value, it doesn't quite work.

I think I'm approaching ths problem the wrong way. Is there some sort of function I can check to see if $items[0]['store-offer_store-name'] actually has a value?

Also when I tried to print out $items[0]['store-offer_store-name'], I got something like this...

Array['store-offer_store-name']

Thanks for any help.

jatar_k

6:27 am on Oct 21, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



have you tried

if ($items[0]['store-offer_store-name'] == "" ¦¦!isset($items[0]['store-offer_store-name']))

that echo looks funny though

how are you setting it?

jamie

5:39 pm on Oct 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



jatar_k

i don't think you need to do any!isset() or == "" to check the value of any variable, including part of an array.

if the value of the $var is 0, empty, NULL or FALSE, it returns a false value, otherwise it returns true,

so the simple statement if ($var) or if (!$var) is enough without all the isset, etc

re: irock's array, it looks like there is something wrong with the array itself as opposed to the checking?

coopster

5:55 pm on Oct 21, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



irock, use the print_r function to print out your arrays (works on all arrays, including multi-dimensional). Using the <pre> tags will print it out nicer:

<?php
print '<pre>';
print_r($items);
print '</pre>';
?>

This should show you your values.

jatar_k

6:44 pm on Oct 21, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



$var is 0, empty, NULL or FALSE,

true but I usually test each individually as each of those cases could denote a different behaviour.