2015-10-15 98 views
-1

我对PHP和Web开发很陌生。我正在努力研究如何使用for循环在PHP中最好地编程这个多维数组。PHP Help在For循环中构建多维数组

本质上,它是一个评论网站。我正在使用Bootstrap在不同的选项卡中呈现数据。数据正在通过jquery ajax请求调用,并且PHP页面正在返回JSON。 Handlebars正被用于渲染模板中的数据。

最后,我想大致这种格式发送数据:

{ 
    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] 

什么我想,虽然实现的是,每一个“问题”部分被包裹在它自己的阵列。像:

"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]] 

然后我可以添加一个一致的密钥给他们每个人。

回答

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) 
+0

谢谢你,谢谢你,谢谢你!就是这样! – user1557966

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)); 
}