多维数组/创建调查

发布于 2024-10-12 06:16:56 字数 802 浏览 4 评论 0原文

我有一个像这样的数组:

$survey = array(
        'Category1' => array(
            'Question1' => array(
                'Option1', 'Option2', 'Option3'
            ),
            'Question2' => array(
                'Option1', 'Option2', 'Option3'
            )
        ),
        'Category2' => array(
            'Question1' => array(
                'Option1', 'Option2', 'Option3'
            ),
            'Question2' => array(
                'Option1', 'Option2', 'Option3'
            )
        )
    );

这个数组实际上要大得多。要求是每页 3 个问题。我的想法是存储我当前所在的类别和问题。例如类别 0,问题 2。然后检查 array_key_exists 是否存在,如果存在,则显示,如果不存在,则递增并重试。正如您可能已经猜到的,类别和问题没有键(至少没有供我循环使用的数字键)。因此,据我所知,使用索引是不可能的。例如,如何在不知道类别 2 的值是什么的情况下,每页动态显示 3 个问题,并自动获取下一页的下 3 个问题。我怎样才能遍历/瞄准这个?

谢谢, 瑞安

I have an array like this:

$survey = array(
        'Category1' => array(
            'Question1' => array(
                'Option1', 'Option2', 'Option3'
            ),
            'Question2' => array(
                'Option1', 'Option2', 'Option3'
            )
        ),
        'Category2' => array(
            'Question1' => array(
                'Option1', 'Option2', 'Option3'
            ),
            'Question2' => array(
                'Option1', 'Option2', 'Option3'
            )
        )
    );

This array is in practice much larger. The requirement is 3 questions per page. My thought was to store which category and question I'm currently on. For example category 0, question 2. Then check to see if array_key_exists and if so, display, if not, increment and try again. As you might have guessed, categories and questions don't have keys (at least not numeric ones for me to loop through). So using an index is, as far as I know, is out of the question. How can I dynamically display 3 questions per page and automatically get the next 3 questions for the next page without knowing what the value is for category2, for example. How can I traverse/target this?

Thanks,
Ryan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

℉絮湮 2024-10-19 06:16:56

数据看起来相当静态,所以我建议更改数据格式:)

将数组更改为类似以下内容:

$survey = array(
   array( 'name' = > 'Category1', 
          'questions' => array(
            array(
               'name' => 'Question1',
               'opts' => array(
                  'Option1', 'Option2', 'Option3'
               )
            ),
                    array(
               'name' => 'Question2',
               'opts' => array(
                  'Option1', 'Option2', 'Option3'
               )
            )
        ),
   array( 'name' = > 'Category2', 
          'questions' => array(

            array(
               'name' => 'Question1',
               'opts' => array(
                  'Option1', 'Option2', 'Option3'
               )
            ),
            array(
               'name' => 'Question2',
               'opts' => array(
                  'Option1', 'Option2', 'Option3'
               )
            )
        )
    );

然后您可以使用整数索引。只需记住 2 个数字(类别索引和类别内的问题索引。并且在每种情况下都递增直到数组末尾。PHP

不是我最强的语言,因此上面的代码对于本地 PHP 程序员来说可能看起来很奇怪。但是根本原因OP 的困难之一是无法轻松创建迭代器类型对象,这是因为基于键的数组具有由其哈希映射性质给出的“奇怪”顺序,并允许您构建类似迭代器的对象。 (又名数组索引)。

The data seems fairly static so i would suggest changing the data format :)

Change the array into something like:

$survey = array(
   array( 'name' = > 'Category1', 
          'questions' => array(
            array(
               'name' => 'Question1',
               'opts' => array(
                  'Option1', 'Option2', 'Option3'
               )
            ),
                    array(
               'name' => 'Question2',
               'opts' => array(
                  'Option1', 'Option2', 'Option3'
               )
            )
        ),
   array( 'name' = > 'Category2', 
          'questions' => array(

            array(
               'name' => 'Question1',
               'opts' => array(
                  'Option1', 'Option2', 'Option3'
               )
            ),
            array(
               'name' => 'Question2',
               'opts' => array(
                  'Option1', 'Option2', 'Option3'
               )
            )
        )
    );

And you can use integer indexes then. Just remember 2 number (the category index and the question index inside the category. And just increment until end of array in each case.

Php is not my strongest language so the code above might look strange to a native php programmer. However the root cause of OP's difficulties is the inability to easily create an interator type object. This is because of the fact that the key based array have a "strange" order given by their hash map nature. Change the nature and allow yourself to build an interator like object (aka an array index).

貪欢 2024-10-19 06:16:56

由于您使用的是关联数组(也称为哈希),因此它没有顺序。每个问题和每个类别都需要有下一个问题/类别键。之后,请参阅链接表算法。

Since you're using an associative array (aka hash), there is no order to it. Each question and each category need to have the next question/category key with them. After that, see link-list algorithms.

浴红衣 2024-10-19 06:16:56

我的array_keys()函数会对你有帮助吗?您将迭代键数组(以获取下一个键)。

My be array_keys() function will help you? You will iterate keys array (to get next keys).

雨后彩虹 2024-10-19 06:16:56
<?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 '</pre></body></html>';

?>

输出:

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>';
<?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 '</pre></body></html>';

?>

And the output:

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

There's my bid. Returns an array similar to your original array with the questions that should be displayed on that specific page.

If you want a more visual representation:

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>';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文