Question

I have an array $fping and when its key is equal to a variable($probe_name), how to get the exact value from that array.

$fping=array(
    'FPing1'=>$x1,
    'FPing3'=>$x3,
    'FPing5'=>$x5,
    'FPing10'=>$x10,
    'FPing30'=>$x30,
    'FPing60'=>$x60,
    'FPing120'=>$x120,
    'FPing180'=>$x180,
    'FPing300'=>$x300
);

if(array_key_exists($probe_name,$fping)){
    //how to get $x.. value when above if is ok 
}

(The variable $probe_name gets its value time to time as $fping gets its keys. Such as FPing1,FPing3...).

Can I use variable name as a string in aray_key_exists()?

Was it helpful?

Solution

$result = array();
$fping=array(
    'FPing1'=>$x1,
    'FPing3'=>$x3,
    'FPing5'=>$x5,
    'FPing10'=>$x10,
    'FPing30'=>$x30,
    'FPing60'=>$x60,
    'FPing120'=>$x120,
    'FPing180'=>$x180,
    'FPing300'=>$x300);

if(array_key_exists($probe_name,$fping)){
  $result[] = $fping[$probe_name];
}

or

$result = array();
$fping=array(
             'FPing1'=>$x1,
             'FPing3'=>$x3,
             'FPing5'=>$x5,
             'FPing10'=>$x10,
             'FPing30'=>$x30,
             'FPing60'=>$x60,
             'FPing120'=>$x120,
             'FPing180'=>$x180,
             'FPing300'=>$x300);

foreach ( $fping as $key => $value ) {
  if ( $key == $probe_name ) {
    $result[] = $value;
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top