1

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()?

4

1 回答 1

1
$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];
}

或者

$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;
  }
}
于 2013-05-23T10:15:36.927 回答