找到解决方案并投票
这是我的代码:
//go through each question
foreach($file_data as $value) {
//separate the string by pipes and place in variables
list($title, $content, $date_posted) = explode('|', $value);
//create an associative array for each input
$file_data_array['title'] = $title;
$file_data_array['content'] = $content;
$file_data_array['date_posted'] = $date_posted;
}
发生的事情是关联值不断被删除。有没有办法可以将值附加到数组?如果没有,我还能怎么做?
最佳答案
您可以使用类似这样的方式附加到 $file_data_array
数组:
foreach($file_data as $value) {
list($title, $content, $date_posted) = explode('|', $value);
$item = array(
'title' => $title,
'content' => $content,
'date_posted' => $date_posted
);
$file_data_array[] = $item;
}
(可以避免使用临时的$item
变量,同时进行数组的声明和$file_data_array
末尾的矫揉造作)
有关更多信息,请查看手册的以下部分:Creating/modifying with square bracket syntax
关于php - 使用 foreach 向关联数组添加一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5493062/