Adding an item to an associative array

后端 未结 5 1385
太阳男子
太阳男子 2020-12-12 20:02
//go through each question
foreach($file_data as $value) {
   //separate the string by pipes and place in variables
   list($category, $question) = explode(\'|\', $v         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-12 20:35

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

提交回复
热议问题