私は次のPHP関数を持っています:
public function createOptions($options, $cfg=array()) {
$cfg['methodKey'] = isset($cfg['methodKey']) ? $cfg['methodKey'] : 'getId';
$cfg['methodValue'] = isset($cfg['methodValue']) ? $cfg['methodValue'] : 'getName';
$cfg['beforeKey'] = isset($cfg['beforeKey']) ? $cfg['beforeKey'] : '';
$cfg['beforeValue'] = isset($cfg['beforeValue']) ? $cfg['beforeValue'] : '';
$cfg['afterKey'] = isset($cfg['afterKey']) ? $cfg['afterKey'] : '';
$cfg['afterValue'] = isset($cfg['afterValue']) ? $cfg['afterValue'] : '';
$array = array();
foreach ($options as $obj) {
$array[$cfg['beforeKey'] . $obj->$cfg['methodKey']() . $cfg['afterKey']] = $cfg['beforeValue'] . $obj->$cfg['methodValue']() . $cfg['afterValue'];
}
return $array;
}
これは、配列データから選択ボックスを作成するためにアプリの周りで使用するものです。最近、選択ボックスのキーと値の前後に文字列を追加するための4つの新しい$cfg変数を追加しました。したがって、たとえば、ドロップダウンリストがデフォルトで「A、B、C」のようになっている場合は、次のように渡すことができます。
$cfg['beforeValue'] = 'Select ';
$cfg['afterValue'] = ' now!';
「今すぐAを選択!、今すぐBを選択!、今すぐCを選択!」を取得します。
したがって、これは正常に機能しますが、PHPには、2行ではなく1行でこれを実現する方法があるのではないかと思いました。これを行うには特別な方法があるに違いないと思います。