P粉3109311982023-08-07 11:24:14
$user = array( 'name' => 'John', 'age' => 35 ); // echo key($user)." is ".($user[key($user)]).","; $counter = count($user); $i = 1; $res = ''; foreach ($user as $key => $value) { $res .= $key. " is ". $value; if ($counter != $i) { $res = $res .", "; } $i = $i + 1; } echo $res;
P粉8264299072023-08-07 09:41:59
$values =[]; foreach ($user as $key => $value) { $values[] = "{$key} is {$value}"; } echo implode(', ', $values);
或
$values = array_map(fn($key, $value) => "{$key} is {$value}", array_keys($user), $user); echo implode(', ', $values);