2

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

1
  • What is the result getting by your approach? Commented Aug 24, 2018 at 11:44

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Looks like the most clever answer but I tested and get apple,banana as output (missing orange)
@AymDev True I had to eat some of my comment, one array_flip is needed. Answer edited
Now it makes sense ! +1
2

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

2 Comments

Looks better than my answer, nice one !
You don't need array_flip with array_intersect_key
1

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
)

Comments

1

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
    )
);

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.