Devendra Gokhe
Devendra Gokhe

Reputation: 197

Pass comma separated key string and get value of array according to key in PHP

I am trying to get value from array and pass only comma separated key string and get same output without. Is it possible without using foreach statement. Please suggest me.

<?php
$str = "1,2,3";
$array = array("1"=>"apple", "2"=>"banana", "3"=>"orange");

$keyarray = explode(",",$str);
$valArr = array();
foreach($keyarray as $key){
   $valArr[] = $array[$key];
}
echo $valStr = implode(",", $valArr);    
?>    

Output : apple,banana,orange

Upvotes: 2

Views: 1168

Answers (5)

mickmackusa
mickmackusa

Reputation: 47874

You want to convert a string to another string based on a mapping array. This entire task can be achieved with preg_replace_callback() alone. Demo

Match each whole integer and replace it with its corresponding string in the lookup array.

$str = "1,2,3";
$array = [
    "1" => "apple",
    "2" => "banana",
    "3" => "orange"
];

echo preg_replace_callback(
         '/\d+/',
         fn($m) => $array[$m[0]] ?? $m[0],
         $str
     );
# apple,banana,orange

Upvotes: 0

Andreas
Andreas

Reputation: 23958

Use array_intersect_key

$str = "1,2,3";
$array = array("1"=>"apple", "2"=>"banana", "3"=>"orange");

$keyarray = explode(",",$str);
echo implode(",", array_intersect_key($array, array_flip($keyarray)));

https://3v4l.org/gmcON


One liner:

echo implode(",", array_intersect_key($array, array_flip(explode(",",$str))));

A mess to read but a comment above can explain what it does.
It means you don't need the $keyarray

Upvotes: 2

AymDev
AymDev

Reputation: 7474

Another way could be using array_map():

echo $valStr = implode(",", array_map(function ($i) use ($array) { return $array[$i]; }, explode(",", $str)));

Read it from bottom to top:

echo $valStr = implode(                 // 3. glue values
    ",",
    array_map(                          // 2. replace integers by fruits
        function ($i) use ($array) {
            return $array[$i];
        },
        explode(",", $str)              // 1. Split values
    )
);

Upvotes: 1

Doğan U&#231;ar
Doğan U&#231;ar

Reputation: 186

you can try using array_filter:

$str = "1,2,3";
$array = array("1"=>"apple", "2"=>"banana", "3"=>"orange");

$keyarray = explode(",",$str);

$filtered = array_filter($array, function($v,$k) use($keyarray){
    return in_array($k, $keyarray);
},ARRAY_FILTER_USE_BOTH);

print_r($filtered);

OUTPUT

Array
(
    [1] => apple
    [2] => banana
    [3] => orange
)

Upvotes: 1

GRESPL Nagpur
GRESPL Nagpur

Reputation: 2098

Suggestion : Use separate row for each value, to better operation. Although you have created right code to get from Comma sparate key to Value from array, but If you need it without any loop, PHP has some inbuilt functions like array_insersect , array_flip to same output

$str = "1,2";
$arr1 = ["1"=>"test1","2"=>"test2","3"=>"test3"];
$arr2  = explode(",",$str);
echo implode(", ",array_flip(array_intersect(array_flip($arr1),$arr2)));

Live demo

Upvotes: 2

Related Questions