如何通过php类中的所有函数保留post变量? 我正在函数中访问数组中的post变量。但是当我尝试访问另一个函数中的那些时,它只会重新计算数组中的最后一个元素。
foreach ( $questionAry as $questionID )
{
$questionName = $this->htmlID_questionID . $questionID;
$question_answer = $_POST[$questionName];
$_SESSION['qusID'] = $questionID;
$_SESSION['qusanswer'] = $question_answer;
}
我想在另一个函数中访问$ questionID和$ question_answer。我尝试通过会话来完成,但我只能访问最后一个值。
答案 0 :(得分:0)
你的循环错了......这些人在每次迭代时都会被覆盖
$_SESSION['qusID'] = $questionID;
$_SESSION['qusanswer'] = $question_answer;
您需要使用索引循环,类似于......
foreach ( $questionAry as $key => $questionID )
{
$questionName = $this->htmlID_questionID . $questionID;
$question_answer = $_POST[$questionName];
$_SESSION['qusID'][$key] = $questionID;
$_SESSION['qusanswer'][$key] = $question_answer;
}
编辑:根据下面的对话,尝试类似的事情。
function getvalues() {
$questions = array();
$jsonEncoded = ( get_magic_quotes_gpc() ) ? stripslashes($_POST[$this->htmlID_questionIDAry]) : $_POST[$this->htmlID_questionIDAry];
$jsonDecoded = json_decode($jsonEncoded);
$questionAry = explode(",",$jsonDecoded->list);
$instr = "";
foreach ( $questionAry as $key => $questionID ) {
$questions[$key]['name'] = $this->htmlID_questionID . $questionID;
$questions[$key]['answer'] = $_POST[$questionName];
}
return $questions;
}
$parsedQuestions = getvalues();
name_of_other_function($parsedQuestions);
function name_of_other_function($parsedQuestions){
print_r($parsedQuestions);
}