[ create a new paste ] login | about

Link: http://codepad.org/lwOa7LNI    [ raw code | output | fork ]

PHP, pasted on Apr 19:
<?php 

$line = 'What is a dog?, A pet, A human, A house, A train';

    //This code assumes no commas in the question or answers.
    $answers = explode(',', $line);

    //pull the question into a variable
    $question = $answers[0];

    //remove the question from the list of answers
    unset($answers[0]);

    //add the array to your result array
    $result[]=array('question'=>$question, 'answers'=>$answers );

$line = 'What is a car?, A vehicle, A tool, A number, A person';

    //This code assumes no commas in the question or answers.
    $answers = explode(',', $line);

    //pull the question into a variable
    $question = $answers[0];

    //remove the question from the list of answers
    unset($answers[0]);

    //add the array to your result array
    $result[]=array('question'=>$question, 'answers'=>$answers );
    var_dump($result);
?>


Output:
array(2) {
  [0]=>
  array(2) {
    ["question"]=>
    string(14) "What is a dog?"
    ["answers"]=>
    array(4) {
      [1]=>
      string(6) " A pet"
      [2]=>
      string(8) " A human"
      [3]=>
      string(8) " A house"
      [4]=>
      string(8) " A train"
    }
  }
  [1]=>
  array(2) {
    ["question"]=>
    string(14) "What is a car?"
    ["answers"]=>
    array(4) {
      [1]=>
      string(10) " A vehicle"
      [2]=>
      string(7) " A tool"
      [3]=>
      string(9) " A number"
      [4]=>
      string(9) " A person"
    }
  }
}


Create a new paste based on this one


Comments: