[Solved] Arrays with keys to custom string php [duplicate]


Using implode and array_map

$input = [13 => 500, 16 => 1000];
$output = implode(', ', array_map(
                function ($v, $k) {
            return $k . " " . $v;
        }, $input, array_keys($input))
);
var_dump($output);

Using foreach

$input = [13 => 500, 16 => 1000];
$output = "";
foreach ($input as $k => $v) {
    $output .= $k . " " . $v . ", ";
}
$output = rtrim($output, ", ");
var_dump($output);

5

solved Arrays with keys to custom string php [duplicate]