将多个关联分配给一个
Assigning multiple associative to one
我正在尝试添加一个问题,对于该问题我有 4 个选项。在这些选项中,当然只有一个是正确的。但是,提交表格后,我收到如下数据:
Array
(
[option_A] => answera
[option_B] => answerb
[option_C] => answersc
[option_D] => answerd
)
我想用一个键重新分配这些数组,如下所示:
Array
(
[answers] => Array
(
[0] => answerd
[1] => answerb
[2] => answerc
[3] => answerd
)
)
是否有可能得到我期待的结果?这可以通过函数 array_walk()
实现吗?
您可以如下使用。通过创建新数组。
<?php
$Output = array();
$input = array("option_A" => "answera", "option_B" => "answerb", "option_C" => "answersc"
, "option_D" => "answerd");
foreach ($input as $key => $value)
{ $PArray[] = $value; }
$Output["answers"] = $PArray;
echo "<pre>";print_r($Output);exit();
?>
我正在尝试添加一个问题,对于该问题我有 4 个选项。在这些选项中,当然只有一个是正确的。但是,提交表格后,我收到如下数据:
Array
(
[option_A] => answera
[option_B] => answerb
[option_C] => answersc
[option_D] => answerd
)
我想用一个键重新分配这些数组,如下所示:
Array
(
[answers] => Array
(
[0] => answerd
[1] => answerb
[2] => answerc
[3] => answerd
)
)
是否有可能得到我期待的结果?这可以通过函数 array_walk()
实现吗?
您可以如下使用。通过创建新数组。
<?php
$Output = array();
$input = array("option_A" => "answera", "option_B" => "answerb", "option_C" => "answersc"
, "option_D" => "answerd");
foreach ($input as $key => $value)
{ $PArray[] = $value; }
$Output["answers"] = $PArray;
echo "<pre>";print_r($Output);exit();
?>