将数组值分配给另一个数组的子数组

时间:2014-11-07 14:05:13

标签: php arrays

我在$ xml-> channel->项目中有一个数组,如果我运行,它会正确输出:

foreach ($xml->channel->item as $entry){    
    echo $entry->title;
    echo $entry->category;
    echo $entry->pubDate;
}

现在,我正在努力使这个数组使用以下形式,它输出一个格式化的表:

$rows = array(
 'row[0]' => array('title' => 'Test Title','category' => 'Computer', 'date' => 'test date'),
 'row[1]' => array('title' => 'Test Title 2','category' => 'Chemical', 'date' => 'test date'),
);

$form['table'] = array(
                '#type' => 'tableselect',
                '#header' => $header,
                '#options' => $rows,
        );

我尝试了这段代码,但它没有工作:

$i=0;
foreach ($xml->channel->item as $rows){
    row[$i][title] = $rows->title;
    row[$i][category] = $rows->category;
    row[$i][date] = $rows->pubDate;
    $i=++;
}

解决方案:

将我从下面的评论中找到的解决方案复制到帖子正文:

$rows = array(); 
foreach ($xml->channel->item as $entry) {
   $row['title'] = $entry->title;
   $row['category'] = $entry->category;
   $row['date'] = $entry->pubDate;

 array_push($rows, $row);
}

1 个答案:

答案 0 :(得分:0)

终于想通了。我希望这会拯救其他人完成同样的任务:

$rows = array();
  foreach ($xml->channel->item as $entry) {    
   $row['title'] = $entry->title;
   $row['category'] = $entry->category;
   $row['date'] = $entry->pubDate;
array_push($rows, $row);
}