Need help to grouping php arrays to make it better -
first of all, grouping working sense dirty. need create looks clean , better. have next foreach
$data['new_array'] = array(); //i have utilize $data['new_array'] because need pass template. foreach ($get_topics $topic) { //is possible create 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 subarray grouping, works need improve solutions //$new_array[$topic['tid']]['vid'][$topic['vid']][] = $topic['vid']; //$new_array[$topic['tid']]['vid'][$topic['vid']][] = $topic['yesno']; }
i wouldn't seek create shorter, here's code in looking version.
$data['new_array'] = array(); foreach ($get_topics $topic) { $data['new_array'][$topic['tid']] = array( 'tid' => $topic['tid'], 'title' => $topic['title'], 'yes' => $topic['yes'], 'no' => $topic['no'] ); }
not sure type $topic['tid'] should careful when using non-consecutive numbers array keys.
php arrays multidimensional-array
No comments:
Post a Comment