Array
(
[0] => Array
(
[gr_number] => 1
[batch] => A
[maths] => 80/100
[english] => 80/100
[science] => 80/100
[hindi] => 80/100
[computer] => 80/100
[socialscience] => 85/100
)
[1] => Array
(
[gr_number] => 2
[batch] => A
[maths] => 80/100
[english] => 80/100
[science] => 80/100
[hindi] => 80/100
[computer] => 80/100
[socialscience] => 86/100
)
[2] => Array
(
[gr_number] => 3
[batch] => A
[maths] => 80/100
[english] => 80/100
[science] => 80/100
[hindi] => 80/100
[computer] => 80/100
[socialscience] => 87/100
)
)
正如你所看到的,它们是主数组中的一些suject元素,o我想合并同一个数组中的所有主题元素,就像这样
Array
(
[0] => Array
(
[gr_number] => 1
[batch] => A
[result_details] => maths : 80/100,english:80/100,science:80/100,hindi:80/100,computer:80/100,socialscience:85/100;
)
[1] => Array
(
[gr_number] => 2
[batch] => A
[result_details] => maths : 80/100,english:80/100,science:80/100,hindi:80/100,computer:80/100,socialscience:85/100;
)
[2] => Array
(
[gr_number] => 3
[batch] => A
[result_details] => maths : 80/100,english:80/100,science:80/100,hindi:80/100,computer:80/100,socialscience:85/100;
)
)
答案 0 :(得分:0)
您只需在result_details
循环中添加一个新元素foreach
,其中包含其他元素的组合值,并取消设置您以后不需要的元素:
foreach($arr as &$val){
$val['result_details']='maths:'.$val['maths'].',english:'.$val['english'].',science:'.$val['science'].',hindi:'.$val['hindi'].',computer:'.$val['computer'].',socialscience:'.$val['socialscience'];
unset($val['maths'],$val['english'],$val['science'],$val['science'],$val['hindi'],$val['computer'],$val['socialscience']);
}
<强> Demo 强>
答案 1 :(得分:0)
您可以将array_map
与array_diff_key
和array_intersect_key
结合使用。
不知怎的,这样:
$keys = array('gr_number', 'batch');
$excludes = array_combine($keys, array_fill(0, count($keys), 0));
$result = array_map(
function ($arr) use($excludes) {
$ret = array_intersect_key($arr, $excludes);
$arr = array_diff_key($arr, $excludes);
$ret['result_details'] = implode(', ', array_map(
function ($v1, $v2) { return "$v1:$v2"; },
array_keys($arr), array_values($arr)
));
return $ret;
},
$array
);