Adding an item to an associative array Adding an item to an associative array arrays arrays

Adding an item to an associative array


You can simply do this

$data += array($category => $question);

If your're running on php 5.4+

$data += [$category => $question];


I think you want $data[$category] = $question;

Or in case you want an array that maps categories to array of questions:

$data = array();foreach($file_data as $value) {    list($category, $question) = explode('|', $value, 2);    if(!isset($data[$category])) {        $data[$category] = array();    }    $data[$category][] = $question;}print_r($data);


before for loop :

$data = array();

then in your loop:

$data[] = array($catagory => $question);