【问题标题】:adding a value to an associative array with a foreach?使用 foreach 向关联数组添加值?
【发布时间】:2011-07-26 11:17:04
【问题描述】:

找到解决方案并投票


这是我的代码:

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

}

会发生什么是关联值不断被删除。有没有办法可以将值附加到数组?如果没有,我还能怎么做?

【问题讨论】:

  • 你必须接受正确答案^_^
  • 前 15 分钟没让我。我的评论有点先发制人。 . .

标签: php foreach associative-array


【解决方案1】:

你需要一把额外的钥匙。

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

【讨论】:

  • 我仍然可以使用$file_data_array['title']获得该值?
  • 没有。但是以这种方式访问​​数据是问题的一部分。您可以使用$file_data_array[0]['title'] 访问第一个,$file_data_array[1]['title'] 访问第二个,等等。
【解决方案2】:

试试这个:

$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

【讨论】:

  • 我不喜欢这样将所有标题存储在一起,而不是将标题、内容和 date_posted 存储在同一个键下。不过是偏好问题。
【解决方案3】:

您可以使用以下方式附加到 $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

【讨论】:

  • 谢谢,这正是我要找的!
  • 这是一个非常干净的解决方案。我会投票否决我的解决方案。 =D
【解决方案4】:

是否要将关联数组附加到$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,
    );

}

【讨论】:

  • 完美运行,但排名第二:/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 2016-10-31
  • 2016-10-03
  • 1970-01-01
  • 1970-01-01
  • 2012-12-24
  • 1970-01-01
相关资源
最近更新 更多