0

首先,我的分组正在工作,但我觉得它很脏。需要有人让它看起来更干净更好。我有以下 foreach

$data['new_array'] = array();  //I have to use $data['new_array'] because I need to pass it to template.
foreach ($get_topics as $topic) {
    //Is that possible to make these 4 lines shorter?
    $data['new_array'][$topic['tid']]['tid'] = $topic['tid'];   
    $data['new_array'][$topic['tid']]['title'] = $topic['title'];
    $data['new_array'][$topic['tid']]['yes'] = $topic['yes'];
    $data['new_array'][$topic['tid']]['no'] = $topic['no'];

    //The belows are subarray grouping, it actually works but I need better solutions
    //$new_array[$topic['tid']]['vid'][$topic['vid']][] = $topic['vid'];
    //$new_array[$topic['tid']]['vid'][$topic['vid']][] = $topic['yesno'];
}
4

1 回答 1

0

我什至不会试图让它更短,但这是你的代码在一个好看的版本中。

$data['new_array'] = array();

foreach ($get_topics as $topic) {
    $data['new_array'][$topic['tid']] = array(
        'tid' => $topic['tid'],
        'title' => $topic['title'],
        'yes' => $topic['yes'],
        'no' => $topic['no']
    );
}

不确定 $topic['tid'] 是什么类型,但在使用非连续数字作为数组键时应该小心。

于 2013-02-07T10:48:44.223 回答