我有以下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!';并获得“立即选择!”,现在选择B!,现在选择C!
所以这工作正常,但我想知道是否有某种方式在PHP中完成这一行,而不是两个。 我认为必须有一种特殊的方式来做到这一点。
I have the following PHP function:
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; }It's something I use around my app to create select boxes from array data. I just recently added 4 new $cfg variables for adding strings before or after the key and value of the select box. So for example, if my drop down list looked like "A, B, C" by default, I can pass:
$cfg['beforeValue'] = 'Select '; $cfg['afterValue'] = ' now!';and get "Select A now!, Select B now!, Select C now!"
So this works fine, but I was wondering if there was some way in PHP to accomplish this in one line sorta speak rather than two. I figure there must be a special way to do this.
最满意答案
首先,用这个来简化那些可怕的代码:
public function createOptions($options, array $cfg = array()) { $cfg += array( 'methodKey' => 'getId', 'methodValue' => 'getName', ... );不需要所有的isset和重复的键名,一个简单的数组联合就可以做到。
其次,你可以使用类似sprintf东西:
$cfg['surroundingValue'] = 'Select %s now!'; echo sprintf($cfg['surroundingValue'], $valueInTheMiddle);First, simplify that horrendous code with this:
public function createOptions($options, array $cfg = array()) { $cfg += array( 'methodKey' => 'getId', 'methodValue' => 'getName', ... );No need for all the isset and repeated key names, a simple array union will do.
Secondly, you can use something like sprintf:
$cfg['surroundingValue'] = 'Select %s now!'; echo sprintf($cfg['surroundingValue'], $valueInTheMiddle);更多推荐
发布评论