如何检查PHP数组的密钥然后赋值?

Say I have an array called $myarray like this...

[187] => 12
[712] => 24

Within a loop later in my code, i want to check to see if a key exists in that array and if it does, i want to assign the corresponding value to a variable.

So I'd check like this...

if (array_key_exists($id), $myarray)) { 
            $newvariable= "(the value that goes with the index key)";
            } else {  
            $newvariable="";
            }

So if it checked for key "187", then $newvariable would be assigned "12";

I guess I just need to know what to replace "(the value that goes with the index key)" with. Or maybe I'm approaching it wrong?

Just use the value of $id as key:

if (array_key_exists($id, $myarray)) { 

            $newvariable= $myarray[$id];

      } else {  

            $newvariable="";

       }
<?php

$myArr = [187 => 12, 712 => 24];

foreach($myArray as $key => $value)
{
   if($key == 187) {
      $newVariable = $value;
   }

}