2011-03-30 103 views
6

发现的解决方案和投票用foreach将值添加到关联数组中?


这里是我的代码:

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

} 

什么情况是,assoc命令值继续得到清除。有没有一种方法可以将值附加到数组中?如果不是,我还能怎么做?

+0

你必须接受正确的答案^ _^ – Neal 2011-03-30 22:23:08

+0

没有让我的第15分钟。我的评论有点抢先。 。 。 – Phil 2011-03-30 22:27:24

+0

啊好吧。得到它 – Neal 2011-03-30 22:28:25

回答

7

您合作ULD追加到$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

+0

谢谢你,那就是我一直在寻找的! – Phil 2011-03-30 22:18:19

+0

这是一个非常干净的解决方案。我会对我的解决方案进行投票。 = D – Ryre 2011-03-30 22:18:47

0

您需要一个额外的密钥。

//go through each question 
$x=0; 
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[$x]['title'] = $title; 
    $file_data_array[$x]['content'] = $content; 
    $file_data_array[$x]['date_posted'] = $date_posted; 
    $x++; 
}  
+0

我仍然可以通过使用'$ file_data_array ['title']'接收该值? – Phil 2011-03-30 22:12:49

+0

不是。但以这种方式访问​​数据是问题的一部分。你可以用'$ file_data_array [0] ['title']'为第一个,'$ file_data_array [1] ['title']'为第二个等等来访问它。 – Ryre 2011-03-30 22:16:51

0

试试这个:

$file_data_array = array(
    'title'=>array(), 
    'content'=>array(), 
    'date_posted'=>array() 
); 
//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 = array(
    'title' => array ('t1', 't2'), 
    'content' => array ('c1', 'c2'), 
    'date_posted' => array ('dp1', 'dp2') 
) 

这里是它的一个演示:

http://codepad.org/jdFabrzE

+0

我不喜欢这个存储所有标题,而不是在相同的密钥下存储标题,内容和日期。尽管优先事项。 – Ryre 2011-03-30 22:18:16

+0

@Toast是的,是的它是^ _^ – Neal 2011-03-30 22:19:51

1

你想关联数组追加到$file_data_array

如果是这样的:

//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[] = array(
     "title" => $title, 
     "content" => $content, 
     "date_posted" => $date_posted, 
    ); 

} 
+0

完美的作品,但第二:/ – Phil 2011-03-30 22:18:35