「PHP」compact関数で変数から連想配列を作成する

2020年10月16日

説明
compact ( mixed $varname1 [, mixed $… ] ) : array
追加された全ての変数を値とする出力配列を返します。
PHPコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
$strA = "A01";
$strB= "B02";
$strC = "C03";
$cfts = array("strA", "strB");
$result = compact("strC", $cfts);
foreach($result as $res){
print_r($res."<br>\n");
}
?>
<?php $strA = "A01"; $strB= "B02"; $strC = "C03"; $cfts = array("strA", "strB"); $result = compact("strC", $cfts); foreach($result as $res){ print_r($res."<br>\n"); } ?>
<?php
$strA = "A01";
$strB= "B02";
$strC = "C03";

$cfts = array("strA", "strB");

$result = compact("strC", $cfts);
foreach($result as $res){
    print_r($res."<br>\n");
}

?>

結果
C03
A01
B02

PHP

Posted by arkgame