我对PHP和Web开发很新。我正在努力解决如何使用for循环在PHP中最好地编程这个多维数组。
基本上,它是一个评论网站。我正在使用Bootstrap在不同的选项卡中呈现数据。通过jquery ajax请求调用数据,PHP页面返回JSON。句柄用于在模板中呈现数据。
最后,我想以大致这种格式发送数据:
{
review: {
reviewstotal: INT,
count: INT,
summary: {
//iterated
tab: INT,
[{
Question:,
Value:,
Color: ,
Percent:,
},
{
Question:,
Value:,
Color: ,
Percent:,
}]
}
}
}
我挣扎的地方在"摘要"部分。 "问题"在数组中,我有"值"作为另一个阵列。颜色值来自if语句,检查" Value"并在模板中设置一个css值。百分比是"价值"乘以10.这是我到目前为止生成数组的原因。
$array = array();
$color ="";
for ($x = 0; $x <= 5; $x++) {
$summary= [];
if ( ${"q{$x}"} <= 40){
$color = "danger";
} else if ( ${"q{$x}"} >= 70){
$color = "success";
} else {
$color = "warning";
}
$summary = array_push ($array, $aquest[$x], ${"q{$x}"}, $color, ${"q{$x}"}*10);
}
我得到的输出是:
"summary": ["The facilities of the school adequately provide for a positive learning/working experience.",null,"danger",0,
"School provides adequate access to teaching materials without any extra expense by the teacher (books, lab supplies, art supplies, materials, copying, etc.)","9.50","danger",95,
"School is well funded to provide students and faculty with adaquate materials to support their course offering.","9.50","danger",95,
"Parent community is actively involved in supporting the school's mission and their child's education endeavors.","9.00","danger",90,
"Classroom student to teacher ratios are kept low.","8.75","danger",87.5,null,"7.63","danger",76.3]
我想要实现的是每个&#34;问题&#34; section包含在它自己的数组中。像:
"summary": [["The facilities of the school adequately provide for a positive learning/working experience.",null,"danger",0],
["School provides adequate access to teaching materials without any extra expense by the teacher (books, lab supplies, art supplies, materials, copying, etc.)","9.50","danger",95],
["School is well funded to provide students and faculty with adequate materials to support their course offering.","9.50","danger",95],
["Parent community is actively involved in supporting the school's mission and their child's education endeavors.","9.00","danger",90],
["Classroom student to teacher ratios are kept low.","8.75","danger",87.5,null,"7.63","danger",76.3]]
然后我可以为每个人添加一致的密钥。
答案 0 :(得分:1)
您将尝试以下内容 -
$array = array();
$color ="";
//taking this out from the scope of loop
$summary= [];
for ($x = 0; $x <= 5; $x++) {
if ( ${"q{$x}"} <= 40){
$color = "danger";
} else if ( ${"q{$x}"} >= 70){
$color = "success";
} else {
$color = "warning";
}
//this is where the diference lies. $summary is a multi-dimension array.
//which means, each element of that array is an array itself.
//So this is how we represent such stuff in php.
//Element at index $x, is also defined as an array with keys containing the
//required values.
$summary[$x] = array(
"text" => $aquest[$x],
"somekey1" => ${"q{$x}"},
"somekey2" => $color,
"somekey3" => ${"q{$x}"}*10
);
}
//here you can print the summary.
print_r( $summary)
答案 1 :(得分:0)
您希望将数组添加到数组中,但目前您正在向数组中添加变量。因此,您需要将$aquest[$x], ${"q{$x}"}, $color, ${"q{$x}"}*10
包装在一个数组中。
$array = array();
$color ="";
for ($x = 0; $x <= 5; $x++) {
$summary= [];
if ( ${"q{$x}"} <= 40){
$color = "danger";
} else if ( ${"q{$x}"} >= 70){
$color = "success";
} else {
$color = "warning";
}
$summary = array_push ($array, array($aquest[$x], ${"q{$x}"}, $color, ${"q{$x}"}*10));
}