寻找一种优雅的方法来重写这个混乱的PHP,显然有很多行如果没有,更短的东西和更甜的将是很棒的。
Meteor.publish("files", function(sessionId) {
var self = this;
// Here I clean all the files I need to remove because the user has
// not submitted the current form.
self.onStop(function () {
console.log (sessionId + " is cleaning...");
cleanFiles(sessionId)
});
// I look for files related to the current upload session only
if(Users.isInRoles(this.userId, ["user"])) {
return Files.find({"session_id":sessionId, "owner":this.userId}, {});
}
//and I make my publication available in case the if failed
return self.ready();
});
输入:
foreach ( $respArr[ 'opt' ] as $k => $v ) {
if ( $v == 'AirbagsFront' ) { $car[ 'opt_o' ][ 'Airbags' ] = 1; } else
if ( $v == 'AlarmSystem' ) { $car[ 'opt_o' ][ 'Alarm' ] = 1; }
}
预期产出:
["AlarmSystem","AirbagsFront"]
希望你不要介意我展示json而不是数组。
答案 0 :(得分:1)
这是使用关联数组定义地图的一种方法,这样可以轻松添加/删除映射:
$map = array(
'AirbagsFront' => 'Airbags',
'AlarmSystem' => 'Alarm',
);
foreach ($respArr['opt'] as $v) {
if(isset($map[$v])) {
$car['opt_o'][$map[$v]] = 1;
}
}