2011-01-14 2 views
2

Ich habe ein Array wie folgt:Multidimensionale Array/Erstellen einer Umfrage

$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' 
      ) 
     ) 
    ); 

Dieses Array ist in der Praxis viel größer. Die Anforderung besteht aus 3 Fragen pro Seite. Mein Gedanke war, zu speichern, in welcher Kategorie und in welcher Frage ich mich gerade befinde. Zum Beispiel Kategorie 0, Frage 2. Dann prüfen, ob array_key_exists und wenn ja, Display, wenn nicht, inkrementieren und erneut versuchen. Wie Sie vielleicht vermutet haben, haben Kategorien und Fragen keine Schlüssel (zumindest keine numerischen für mich). Die Verwendung eines Index ist also, soweit ich weiß, nicht in Frage. Wie kann ich dynamisch 3 Fragen pro Seite anzeigen und automatisch die nächsten 3 Fragen für die nächste Seite erhalten, ohne zu wissen, was der Wert für Kategorie 2 ist. Wie kann ich das durchqueren/anvisieren?

Danke, Ryan

+0

Wie kann man ein Array mit Category1 als Schlüssel zweimal haben kann? – profitphp

+0

Ich nehme an, ist wahrscheinlich ein Tippfehler. :). –

+0

Gibt es einen Grund, warum Sie diese nicht in einer Datenbank speichern können? wäre viel einfacher. – dqhendricks

Antwort

0

Die Daten relativ statisch scheint so würde vorschlagen, i das Datenformat zu ändern :)

ändern das Array in so etwas wie:

$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' 
       ) 
      ) 
     ) 
    ); 

Und Sie können ganze Zahl verwenden Indizes dann. Merken Sie sich nur 2 Nummer (der Kategorie-Index und der Frage-Index innerhalb der Kategorie. Und nur bis zum Ende des Arrays in jedem Fall.

Php ist nicht meine stärkste Sprache, so dass der Code oben seltsam für einen nativen PHP-Programmierer aussehen kann. Die Hauptursache der Schwierigkeiten von OP ist jedoch die Unfähigkeit, ein Objekt vom Typ interator einfach zu erzeugen, da das key-basierte Array eine "seltsame" Ordnung hat, die durch die Art ihrer Hash-Map gegeben ist ein interatorähnliches Objekt (aka ein Array-Index)

0

Da Sie ein assoziatives Array (auch bekannt als Hash) verwenden, gibt es keine Reihenfolge für jede Frage und jede Kategorie muss die nächste Frage/Kategorie Schlüssel mit haben Dann sehen Sie sich die Link-List-Algorithmen an:

0

Mein sein array_keys() Funktion wird Ihnen helfen? Sie werden Schlüssel-Array iterieren (um die nächsten Schlüssel zu bekommen).

0
<?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>'; 

>

Und die Ausgabe:

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

Es mein Gebot ist. Gibt ein Array ähnlich dem ursprünglichen Array mit den Fragen zurück, die auf dieser bestimmten Seite angezeigt werden sollen.

Wenn Sie eine visuelle Darstellung:

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>';