1

我有以下 PHP 数组:

$userAnswers = array(   // you would get these values via $_POST["answers"]
    "s1" => array(
        "q1" => $_POST['S1Q1'],
        "q2" => $_POST['S1Q2']
    ),
    "s2" => array(
        "q1" => $_POST['S2Q1'],
        "q2" => $_POST['S2Q2'],
        "q3" => $_POST['S2Q3'],
        "q4" => $_POST['S2Q4']
    ),
    "s3" => array(
        "q1" => $_POST['S3Q1'],
        "q2" => $_POST['S3Q2'],
        "q3" => $_POST['S3Q3'],
        "q4" => $_POST['S3Q4'],
        "q5" => $_POST['S3Q5']
    ),
    "s4" => array(
        "q1" => $_POST['S4Q1'],
        "q2" => $_POST['S4Q2'],
        "q3" => $_POST['S4Q3'],
        "q4" => $_POST['S4Q4']
    ),
    "s5" => array(
        "q1" => $_POST['S5Q1'],
        "q2" => $_POST['S5Q2'],
        "q3" => $_POST['S5Q3'],
        "q4" => $_POST['S5Q4'],
        "q5" => $_POST['S5Q5']
    ),
    "s6" => array(
        "q1" => $_POST['S6Q1'],
        "q2" => $_POST['S6Q2'],
        "q3" => $_POST['S6Q3'],
        "q4" => $_POST['S6Q4'],
        "q5" => $_POST['S6Q5']
    ),
    "s7" => array(
        "q1" => $_POST['S7Q1'],
        "q2" => $_POST['S7Q2'],
        "q3" => $_POST['S7Q3'],
        "q4" => $_POST['S7Q4'],
        "q5" => $_POST['S7Q5'],
        "q6" => $_POST['S7Q6']
    ),
    "s8" => array(
        "q1" => $_POST['S8Q1'],
        "q2" => $_POST['S8Q2'],
        "q3" => $_POST['S8Q3'],
        "q4" => $_POST['S8Q4'],
        "q5" => $_POST['S8Q5']
    ),
    "s9" => array(
        "q1" => $_POST['S9Q1'],
        "q2" => $_POST['S9Q2'],
        "q3" => $_POST['S9Q3']
    ),
    "s10" => array(
        "q1" => $_POST['S10Q1'],
        "q2" => $_POST['S10Q2']
    ),
    "s11" => array(
        "q1" => $_POST['S11Q1'],
        "q2" => $_POST['S11Q2'],
        "q3" => $_POST['S11Q3'],
        "q4" => $_POST['S11Q4'],
        "q5" => $_POST['S11Q5'],
        "q6" => $_POST['S11Q6']
    ),
    "s12" => array(
        "q1" => $_POST['S12Q1'],
        "q2" => $_POST['S12Q2'],
        "q3" => $_POST['S12Q3']
    ),
    "s13" => array(
        "q1" => $_POST['S13Q1'],
        "q2" => $_POST['S13Q2'],
        "q3" => $_POST['S13Q3']
    ),
    "s14" => array(
        "q1" => $_POST['S14Q1'],
        "q2" => $_POST['S14Q2'],
        "q3" => $_POST['S14Q3'],
        "q4" => $_POST['S14Q4']
    ),
    "s15" => array(
        "q1" => $_POST['S15Q1'],
        "q2" => $_POST['S15Q2'],
        "q3" => $_POST['S15Q3'],
        "q4" => $_POST['S15Q4']
    ),
    "s16" => array(
        "q1" => $_POST['S16Q1'],
        "q2" => $_POST['S16Q2']
    ),
    "s17" => array(
        "q1" => $_POST['S17Q1'],
        "q2" => $_POST['S17Q2'],
        "q3" => $_POST['S17Q3'],
        "q4" => $_POST['S17Q4']
    ),
    "s18" => array(
        "q1" => $_POST['S18Q1'],
        "q2" => $_POST['S18Q2']
    ),
    "s19" => array(
        "q1" => $_POST['S19Q1'],
        "q2" => $_POST['S19Q2'],
        "q3" => $_POST['S19Q3']
    ),
    "s20" => array(
        "q1" => $_POST['S20Q1'],
        "q2" => $_POST['S20Q2'],
        "q3" => $_POST['S20Q3']
    ),
    "s21" => array(
        "q1" => $_POST['S21Q1']
    ),
    "s22" => array(
        "q1" => $_POST['S22Q1'],
        "q2" => $_POST['S22Q2']
    ),
    "s23" => array(
        "q1" => $_POST['S23Q1'],
        "q2" => $_POST['S23Q2'],
        "q3" => $_POST['S23Q3']
    ),
    "s24" => array(
        "q1" => $_POST['S24Q1'],
        "q2" => $_POST['S24Q2']
    ),
    "s25" => array(
        "q1" => $_POST['S25Q1'],
        "q2" => $_POST['S25Q2'],
        "q3" => $_POST['S25Q3'],
        "q4" => $_POST['S25Q4']
    ),
    "s26" => array(
        "q1" => $_POST['S26Q1'],
        "q2" => $_POST['S26Q2'],
        "q3" => $_POST['S26Q3'],
        "q4" => $_POST['S26Q4'],
        "q5" => $_POST['S26Q5']
    ),
    "s27" => array(
        "q1" => $_POST['S27Q1'],
        "q2" => $_POST['S27Q2'],
        "q3" => $_POST['S27Q3'],
        "q4" => $_POST['S27Q4'],
        "q5" => $_POST['S27Q5'],
        "q6" => $_POST['S27Q6']
    )
);

我正在尝试转换以下代码以foreach()使代码更小并且文件大小也更小:

$q001 = $_POST['S1Q1'];
$q002 = $_POST['S1Q2'];
$q003 = $_POST['S2Q1'];
$q004 = $_POST['S2Q2'];
$q005 = $_POST['S2Q3'];
$q006 = $_POST['S2Q4'];
$q007 = $_POST['S3Q1'];
$q008 = $_POST['S3Q2'];
$q009 = $_POST['S3Q3'];
$q010 = $_POST['S3Q4'];
$q011 = $_POST['S3Q5'];
$q012 = $_POST['S4Q1'];
$q013 = $_POST['S4Q2'];
$q014 = $_POST['S4Q3'];
$q015 = $_POST['S4Q4'];
$q016 = $_POST['S5Q1'];
$q017 = $_POST['S5Q2'];
$q018 = $_POST['S5Q3'];
$q019 = $_POST['S5Q4'];
$q020 = $_POST['S5Q5'];
$q021 = $_POST['S6Q1'];
$q022 = $_POST['S6Q2'];
$q023 = $_POST['S6Q3'];
$q024 = $_POST['S6Q4'];
$q025 = $_POST['S6Q5'];
$q026 = $_POST['S7Q1'];
$q027 = $_POST['S7Q2'];
$q028 = $_POST['S7Q3'];
$q029 = $_POST['S7Q4'];
$q030 = $_POST['S7Q5'];
$q031 = $_POST['S7Q6'];
$q032 = $_POST['S8Q1'];
$q033 = $_POST['S8Q2'];
$q034 = $_POST['S8Q3'];
$q035 = $_POST['S8Q4'];
$q036 = $_POST['S8Q5'];
$q037 = $_POST['S9Q1'];
$q038 = $_POST['S9Q2'];
$q039 = $_POST['S9Q3'];
$q040 = $_POST['S10Q1'];
$q041 = $_POST['S10Q2'];
$q042 = $_POST['S11Q1'];
$q043 = $_POST['S11Q2'];
$q044 = $_POST['S11Q3'];
$q045 = $_POST['S11Q4'];
$q046 = $_POST['S11Q5'];
$q047 = $_POST['S11Q6'];
$q048 = $_POST['S12Q1'];
$q049 = $_POST['S12Q2'];
$q050 = $_POST['S12Q3'];
$q051 = $_POST['S13Q1'];
$q052 = $_POST['S13Q2'];
$q053 = $_POST['S13Q3'];
$q054 = $_POST['S14Q1'];
$q055 = $_POST['S14Q2'];
$q056 = $_POST['S14Q3'];
$q057 = $_POST['S14Q4'];
$q058 = $_POST['S15Q1'];
$q059 = $_POST['S15Q2'];
$q060 = $_POST['S15Q3'];
$q061 = $_POST['S15Q4'];
$q062 = $_POST['S16Q1'];
$q063 = $_POST['S16Q2'];
$q064 = $_POST['S17Q1'];
$q065 = $_POST['S17Q2'];
$q066 = $_POST['S17Q3'];
$q067 = $_POST['S17Q4'];
$q068 = $_POST['S18Q1'];
$q069 = $_POST['S18Q2'];
$q070 = $_POST['S19Q1'];
$q071 = $_POST['S19Q2'];
$q072 = $_POST['S19Q3'];
$q073 = $_POST['S20Q1'];
$q074 = $_POST['S20Q2'];
$q075 = $_POST['S20Q3'];
$q076 = $_POST['S21Q1'];
$q077 = $_POST['S22Q1'];
$q078 = $_POST['S22Q2'];
$q079 = $_POST['S23Q1'];
$q080 = $_POST['S23Q2'];
$q081 = $_POST['S23Q3'];
$q082 = $_POST['S24Q1'];
$q083 = $_POST['S24Q2'];
$q084 = $_POST['S25Q1'];
$q085 = $_POST['S25Q2'];
$q086 = $_POST['S25Q3'];
$q087 = $_POST['S25Q4'];
$q088 = $_POST['S26Q1'];
$q089 = $_POST['S26Q2'];
$q090 = $_POST['S26Q3'];
$q091 = $_POST['S26Q4'];
$q092 = $_POST['S26Q5'];
$q093 = $_POST['S27Q1'];
$q094 = $_POST['S27Q2'];
$q095 = $_POST['S27Q3'];
$q096 = $_POST['S27Q4'];
$q097 = $_POST['S27Q5'];
$q098 = $_POST['S27Q6'];

$savearray = array($q001, $q002, $q003, $q004, $q005, $q006, $q007, $q008, $q009, $q010, $q011, $q012, $q013, $q014...);

是否可以?

4

1 回答 1

2

在我看来,您只想展平嵌套数组:

$questions = array();
$counter = 0;
foreach($userAnwers as $key => $value){
    foreach($value as $v){
        $counter+=1;
        $questions["{$counter}"] = $v;
    }
}

所以你会得到 $questions["1"] = $_POST["S1Q1"]

于 2013-06-14T16:00:02.910 回答