Jump to content

[PHP] Ternary operator inside foreach loop


Recommended Posts

Posted

Hi, I have problem with using ternary operator inside foreach.

What do I need?

I get $val["extra_flags"] from $chars_raw query, and when it's odd, I need to set $vars["gmState"] = true - BUT! when $val["extra_flags"] is even, I don't want to set anything. Appreciate any help. Thanks.

function listMyChars($id) {
   global $vars, $realmd;
   $chars_raw= $realmd->fetchAll('SELECT * FROM characters.characters WHERE account=%i', $id);
   $chars = array();
   foreach ($chars_raw as $val) {
       $chars["guid"] = $val["guid"];
       $chars["name"] = $val["name"];
       $chars["race"] = getRace($val["race"]);
       $chars["raceid"] = $val["race"];
       $chars["class"] = getClass($val["class"]);
       $chars["classid"] = $val["class"];
       $chars["gender"] = $val["gender"];
       $chars["level"] = $val["level"];
       $chars["money"] = $val["money"];

/* Can use $val["extra_flags"]&1 or $val["extra_flags"]%2 */

       $val["extra_flags"]&1 ? $chars["gmState"] = '1' : '';
       $vars["listMyChars"][] = $chars;
   }
}

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy Terms of Use