我跟随$array
:
Array
(
[0] => Array
(
[cd] => 1675
[amt_1] => 199.50
[fname] => Joe
[lname] => A
)
[1] => Array
(
[cd] => 1675
[amt_1] => 69.90
[fname] => Joe
[lname] => A
)
[2] => Array
(
[cd] => 1676
[amt_1] => 69.90
[fname] => Tracy
[lname] => A
)
[3] => Array
(
[cd] => 1676
[amt_1] => 199.50
[fname] => Tracy
[lname] => A
)
...
)
我要做的就是将它们组合在一起,在这种情况下,通过fname
或cd
将它们组合在一起,这样我就会有:
[0] => Array
(
[cd] => 1676
Array
(
[0] => Array
(
[amt_1] => 199.50
)
[1] => Array
(
[amt_1] => 69.90
)
[fname] => Joe
[lname] => A
)
[1] => Array
(
[cd] => 1676
Array
(
[0] => Array
(
[amt_1] => 199.50
)
[1] => Array
(
[amt_1] => 69.90
)
[fname] => Tracy
[lname] => A
)
........
我似乎无法弄明白。 这不能在mysql中完成,我需要在php中完成。
有什么想法吗?
由于
编辑:我知道结果示例格式不正确,但基本上我想组合fname
,其余结果将它们放在数组中。
编辑:
@Paulo H
有个好主意。我也找到了另一种方法,将它组合在一起而不是将它组合在一起:
$groups = array ();
foreach ( $the_array as $item ) {
$key = $item ['fname'];
if (! isset ( $groups [$key] )) {
$groups [$key] = array ('items' => array ($item ), 'count' => 1 );
} else {
$groups [$key] ['items'] [] = $item;
$groups [$key] ['count'] += 1;
}
}
答案 0 :(得分:1)
试试这个:
function &array_group_value_by($input_array,$value,$by){
$result = array();
foreach($input_array as $array){
if(!isset($result[$array[$by]])){
$result[$array[$by]] = array();
}
foreach($array as $key=>$data){
if((is_string($value) && $key==$value) || (is_array($value) && in_array($key,$value))){
if(!isset($result[$array[$by]][$key])){
$result[$array[$by]][$key] = array();
}
$result[$array[$by]][$key][] = $data;
}else{
$result[$array[$by]][$key] = $data;
}
}
}
return $result;
}
$grouped = array_group_value_by($yourarray,'amt_1','fname');
print_r($grouped);