我有一个二维数组,用于显示调查。该数组包含63行(问题)。每行包含GpDesc(分组即领导特征)和QueDesc(问题)。有9种不同的GpDesc。我想在第一次出现时回显GpDesc,然后回显该组中的问题和单选按钮,直到下一个GpDesc。我知道我需要一个GpDesc数量的外循环和该组内问题的内循环但我不知道如何做到这一点。这是$ varArray的一部分:
echo
有人可以帮忙吗?
答案 0 :(得分:1)
<?php
$survey =
Array(
Array (
'GpID' => 1168,
'GpDesc' => 'LEADERSHIP TRAITS',
'QueID' => 10266,
'QueDesc' => 'Is visionary',
'GpQue' => 0
),
Array (
'GpID' => 1168,
'GpDesc' => 'LEADERSHIP TRAITS',
'QueID' => 10267,
'QueDesc' => 'Possesses high degree of integrity',
'GpQue' => 0
),
Array (
'GpID' => 1169,
'GpDesc' => 'MANAGEMENT TRAITS',
'QueID' => 10277,
'QueDesc' => 'Involves faculty in strategic planning',
'GpQue' => 0
),
Array (
'GpID' => 1169,
'GpDesc' => 'MANAGEMENT TRAITS',
'QueID' => 10278,
'QueDesc' => 'Utilizes departmental advisory group or other clientele in decision making',
'GpQue' => 0
)
);
echo $survey[0]['GpDesc'].PHP_EOL;
$prevGpId = $survey[0]['GpID'];
foreach ($survey as $line) {
if ($line['GpID'] !== $prevGpId) {
echo $line['GpDesc'].PHP_EOL;
$prevGpId = $line['GpID'];
}
echo "\t".$line['QueDesc'].PHP_EOL;
}