打印具有给定字符串的匹配键的所选数组元素

I have a first array with:

$first_array = Array
(
    "1" => "Loading dock",
    "2" => "Forklift"
);

and, a second string with:

either case 1:

$second_string = 1;

or case 2:

$second_string = 2;

or case 3:

$second_string = 1,2;

I want to print value of

  • first array

in which matched with the keys of first array with value of second string.

I have tried this:

$first_array [$second_string];

It works fine with case 1 & 2 for case 3 I need help....!

If you need such situation why not to try something like this:

$first_array = Array
(
"1" => "Loading dock",
"2" => "Forklift"
);

// $new_array = []; // use this if you want to make new array
$second_string = [1];
// $second_string = [1,2];

if(is_array($second_string)) {
  foreach($second_string as $k=>$s) {
     // $new_array[$s] = $first_array[$s];
     echo $first_array[$s]; // if you only want to print value
  }
}

Send your second_string in terms of array which will be best at this situation.