1

I have functions which return different predifined values:

    function a(){
      $value = 'mercedes';
      replacevalues($value);

    }
    function b(){
     $value =  'audi';
     replacevalues($value);
    }
    function c(){
      $value = 'bmw';
     replacevalues($value);
    }

Is there a function in PHP which will do something like this:

function replacevalues($value){
 switch($value){
   case 'mercedes': 
   echo $value = 'topA'
   break;
   case 'audi':
   echo 'topB'
   break 
//and so on
}
}

It would be nice if could define an array like this which i only have to iterate through (Or some php function that already exists)

$a = array('mercedes'=>'topA', 'audi' => 'topB');

So that I could expand the array or change the corresponding replacements.

Regards

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
tkoatl
  • 31
  • 4

1 Answers1

0

The function you are looking for is: array_search

$array = array(0 => 'azul', 1 => 'rojo', 2 => 'verde', 3 => 'rojo');

$key = array_search('verde', $array); // $key = 2;
$key = array_search('rojo', $array);  // $key = 1;
lalengua
  • 509
  • 3
  • 15