Replace the string made up of another key with the corresponding value, or take the values of multiple arrays at once, how to write this in PHP?

question:

$s = "1,2,3,4";
$a = [1=>"", 2=>"", 3=>"", 4=>""];

the most important effect to achieve:

",,,"

above PHP7

Note again: one line of code, one line of code to achieve the effect.

Php
Apr.06,2022

Code

<?php
$s = "1,2,3,4";
$a = [1=>'', 2=>'', 3=>'', 4=>''];
$arr = [];
foreach (explode(',', $s) as $key) {
    $arr[] = $a[$key];
}
echo implode(',', $arr);

result

,,,

you can use array functions

$result = array_map(function ($arrayKey) use ($a) {
    return $a[$arrayKey] ?? null;
}, explode(',', $s));
$result = implode(',', $result);

or

$result = array_reduce(explode(',', $s), function ($result, $item) use ($a) {
    return $result .= "{$a[$item]},";
}, '');
$result = trim($result, ',');

or

$result = array_intersect_key($a, array_combine(explode(',', $s), explode(',', $s)));
$result = implode(',', $result);

there is no more concise way. Php does not provide a function to obtain more than one key value at a time, so it can only be encapsulated by itself. The first of the above methods is recommended, with clear logic and high efficiency

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e54376-45281.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e54376-45281.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?