<code><?php
$survey = array(
'Category1' => array(
'Question1' => array(
'Option1', 'Option2', 'Option3'
),
'Question2' => array(
'Option1', 'Option2', 'Option3'
),
'Question3' => array(
'Option1', 'Option2', 'Option3'
),
'Question4' => array(
'Option1', 'Option2', 'Option3'
)
),
'Category 2' => array(
'Question1' => array(
'Option1', 'Option2', 'Option3'
),
'Question2' => array(
'Option1', 'Option2', 'Option3'
)
),
'Category 3' => array(
'Question1' => array(
'Option1', 'Option2', 'Option3'
),
'Question2' => array(
'Option1', 'Option2', 'Option3'
),
'Question3' => array(
'Option1', 'Option2', 'Option3'
),
)
);
function fetchQuestions($survey, $page, $perPage = 3)
{
$results = Array();
$nCount = 0; $nRead = 0; $nIndex = $page * $perPage;
foreach ($survey as $CategoryName => $Questions)
{
foreach ($Questions as $Question => $Options)
{
if ($nCount >= $nIndex && $nRead < $perPage)
{
if (!isset($results[$CategoryName]))
$results[$CategoryName] = Array();
$results[$CategoryName][$Question] = $Options;
$nRead++;
}
$nCount++;
}
}
return $results;
}
echo '<html><body><pre>';
var_dump(fetchQuestions($survey,0));
var_dump(fetchQuestions($survey,1));
var_dump(fetchQuestions($survey,2));
echo '
';
?>
И вывод:
array(1) {
["Category1"]=>
array(3) {
["Question1"]=>
array(3) {
[0]=>
string(7) "Option1"
[1]=>
string(7) "Option2"
[2]=>
string(7) "Option3"
}
["Question2"]=>
array(3) {
[0]=>
string(7) "Option1"
[1]=>
string(7) "Option2"
[2]=>
string(7) "Option3"
}
["Question3"]=>
array(3) {
[0]=>
string(7) "Option1"
[1]=>
string(7) "Option2"
[2]=>
string(7) "Option3"
}
}
}
array(2) {
["Category1"]=>
array(1) {
["Question4"]=>
array(3) {
[0]=>
string(7) "Option1"
[1]=>
string(7) "Option2"
[2]=>
string(7) "Option3"
}
}
["Category 2"]=>
array(2) {
["Question1"]=>
array(3) {
[0]=>
string(7) "Option1"
[1]=>
string(7) "Option2"
[2]=>
string(7) "Option3"
}
["Question2"]=>
array(3) {
[0]=>
string(7) "Option1"
[1]=>
string(7) "Option2"
[2]=>
string(7) "Option3"
}
}
}
array(1) {
["Category 3"]=>
array(3) {
["Question1"]=>
array(3) {
[0]=>
string(7) "Option1"
[1]=>
string(7) "Option2"
[2]=>
string(7) "Option3"
}
["Question2"]=>
array(3) {
[0]=>
string(7) "Option1"
[1]=>
string(7) "Option2"
[2]=>
string(7) "Option3"
}
["Question3"]=>
array(3) {
[0]=>
string(7) "Option1"
[1]=>
string(7) "Option2"
[2]=>
string(7) "Option3"
}
}
}
Вот моя ставка. Возвращает массив, аналогичный исходному массиву с вопросами, которые должны отображаться на этой конкретной странице.
Если вы хотите более наглядное представление:
echo '<html><body>';
$page = 0;
while (count($matches = fetchQuestions($survey,$page++)) > 0)
{
echo '<div style="background-color:#CCC;">';
echo '<h2>Page '.$page.'</h2>';
echo '<ul>';
foreach ($matches as $Category => $Questions)
{
echo '<li><strong>'.$Category.'</strong>:<ul>';
foreach ($Questions as $Question => $Options)
{
echo '<li><u>'.$Question.'</u><ul>';
foreach ($Options as $Option)
echo '<li>'.$Option.'</li>';
echo '</ul>';
}
echo '</ul></li>';
}
echo '</ul>';
echo '</div>';
}
echo '</body></html>';