0

I have the following code

while($row = $usafisRSP->fetch_assoc())
{

 $hidden_keys  = array('Applicantid', 'unique_num', 'regs_time' ....);
 $hidden_fields = array_intersect_key($row, array_fill_keys($hidden_keys, NULL));

$hidden_values = array();

foreach ($hidden_fields as $key => $value) {
  // fill the values array using the values from fields array
  $hidden_values[$value] =  "$key = ".base64_decode($value)."
"; if(base64_decode($value)== 0) { $hidden_values[$value] = ""; } echo $hidden_values[$value];

The question is about "if($hidden_values[$value] == 0)" ... Basically I want to do not display/echo the $hidden_values[$value] if it's value of $value is 0. Sometimes $value is 0 or some words like (23 avenue).

2
  • 1
    What exactly is the problem? You dont know how to check the contents of the array for 0 values before echoing? Commented Jan 9, 2011 at 14:48
  • What's the problem you're experiencing with your current code? Also, it looks like you're missing a couple of closing braces. Commented Jan 9, 2011 at 14:48

2 Answers 2

1

I think you ran into three catches with PHP type comparisons and equalities:

  1. Any string not beginning with a number will always loosely equal 0. So basically, if(base64_decode($value)== 0) will likely always resolve to true, even if decoded $value is "Adam".
  2. Return value of base64_decode is a string, so if 0 is the result, it will be string 0, not integer 0. This means if(base64_decode($value) === 0) wouldn't even work if decoded $value is "0". Another catch is base64_decode may return false on errors, again failing this strict equality check.
  3. A non-empty string (other than "0") will always loosely equal true. So this is the only comparison you really need for your case.

I think this is what you want, replacing the last 5 lines...

if(base64_decode($value)) echo $hidden_values[$value];
else $hidden_values[$value] = "";

} // closing your for loop
Sign up to request clarification or add additional context in comments.

Comments

0

Is this what you're looking for?

foreach( $hidden_values as $value ) {
    if( $value !== 0 ) {
        echo $value;
    }
}

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.