PHP join関数で配列の要素同士をカンマ区切りで結合するサンプル

環境
PHP 8.1.2
Ubuntu 22.04.1 LTS

構文
$result = join(“,", $対象の配列名);
join()の第1引数にカンマの文字列(",")、第2引数に対象の配列を指定します。
配列(array)の要素同士をカンマ区切りで結合して1つの文字列に変換するには、join()を使います。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
$ns = [ "study", "skill", "become", "smart", "arkgame"];
$result = join(",", $ns);
echo $result
?>
<?php $ns = [ "study", "skill", "become", "smart", "arkgame"]; $result = join(",", $ns); echo $result ?>
<?php

$ns = [ "study", "skill", "become", "smart", "arkgame"];

$result = join(",", $ns);

echo $result

?>

実行結果
study,skill,become,smart,arkgame

PHP

Posted by arkgame