コマンドラインからphpに配列を次のように渡したい:
c:\<path>\php.exe somefile.php --filter array{['name']=>"lion",['category']=>array{['teeth']=>'long_teeth',['height']=>'short'}}
そして今、コードでは、次のようにコマンドラインを通過したときに、変数フィルターを配列として使用したいと考えています:
$opt['filter'] = array {
['name']=>"lion",
['category']=>
array{
['teeth']=>'long_teeth',
['height']=>'short'
}
}
しかし、問題は渡された引数が文字列になり、配列に解析できません。getopt() 関数を使用して、配列変数 $opt の属性としてフィルターを取得しています。
$shortopts = "abc"; // These options do not accept values
$longopts = array(
"filter:", // Required value
);<br>
$opt = getopt($shortopts, $longopts);
実際のシナリオ全体は、変数を配列、文字列、またはブール値として取り、それを別の php スクリプトにそのまま渡し、そのスクリプトを次のような exec 関数を介して呼び出してい
ますexec(c:\<path>\php.exe myphpscript.php --filter $array_variable );
。$array_variable
以前のスクリプトにあったので、そのまま使用できます。