我在尝试存储多项选择题的结果时遇到了一些困难。所以我有2个表[问题]和[答案]。所有问题都存储在问题表中,当我想显示时,我就有了这个
{{!!Form::open(...)!!}}
@foreach ($questions as $question)
<div>{{$question->question}}</div>
<div class="form-group">
{!! Form::label('answer', 'Score') !!}
{{ Form::selectRange(' answer[]', 1, 10) }}
{{ Form::hidden('question_id[]', $question->id) }}
{{ Form::hidden('user_id[]', $userId) }}
</div>
@endforeach
{{!!Form::submit(...)!!}}
{{!!Form::close()!!}}
好的,所以我也传递了question_id和user_id,这对所有问题都是一样的。我的控制器看起来像这样。
public function store(Request $request)
{
$arr_answers = $request->all();
foreach ($arr_answers as $row) {
QuestionResults::create([
'answer' => $row['answer'],
'question_id' => $row['question_id'],
'user_id' => $row['user_id'],
]);
}
return "working";
$ arr_answers返回3个数组,答案,question_id和user_id,都有正确的信息,所以我的思考过程是foreach循环应该为每个值返回一个int但不幸的是它不打球并返回“非法字符串偏移量”。
请有人指出我的解决方案或更好的方法来做到这一点。
答案 0 :(得分:1)
我不了解Laravel,但from logging import getLogger, basicConfig, INFO
import numpy as np
import xgboost as xgb
from sklearn.datasets import load_digits
from sklearn.model_selection import KFold
# Some logging settings
basicConfig(level=INFO)
logger = getLogger(__name__)
def log_evaluation(period=1, show_stdv=True):
"""Create a callback that logs evaluation result with logger.
Parameters
----------
period : int
The period to log the evaluation results
show_stdv : bool, optional
Whether show stdv if provided
Returns
-------
callback : function
A callback that logs evaluation every period iterations into logger.
"""
def _fmt_metric(value, show_stdv=True):
"""format metric string"""
if len(value) == 2:
return '%s:%g' % (value[0], value[1])
elif len(value) == 3:
if show_stdv:
return '%s:%g+%g' % (value[0], value[1], value[2])
else:
return '%s:%g' % (value[0], value[1])
else:
raise ValueError("wrong metric value")
def callback(env):
if env.rank != 0 or len(env.evaluation_result_list) == 0 or period is False:
return
i = env.iteration
if i % period == 0 or i + 1 == env.begin_iteration or i + 1 == env.end_iteration:
msg = '\t'.join([_fmt_metric(x, show_stdv) for x in env.evaluation_result_list])
logger.info('[%d]\t%s\n' % (i, msg))
return callback
rng = np.random.RandomState(31337)
print("Zeros and Ones from the Digits dataset: binary classification")
digits = load_digits(2)
y = digits['target']
X = digits['data']
kf = KFold(n_splits=2, shuffle=True, random_state=rng)
for train_index, test_index in kf.split(X):
param = {'max_depth': 2, 'eta': 0.3, 'silent': 1, 'objective': 'binary:logistic'}
dtrain = xgb.DMatrix(X[train_index], y[train_index])
dtest = xgb.DMatrix(X[test_index], y[test_index])
# specify validations set to watch performance
watchlist = [(dtest, 'eval'), (dtrain, 'train')]
num_round = 2
# add logger
callbacks = [log_evaluation(1, True)]
bst = xgb.train(param, dtrain, num_round, watchlist, callbacks=callbacks)
最有可能返回结构为$arr_answers
数组作为3个子数组和提交。因此,使用相同的密钥访问3个阵列:
$_POST