我正在尝试将多个数组合并为单个数组,并使值唯一。
$array = array (
$array1 = array(
"id" => 205,
"firstName" => "NANCY GEORGE",
"gender" => "F",
"age" => 21,
"alert" => "bmi"
),
$array2 = array(
"id" => 205,
"firstName" => "NANCY GEORGE",
"gender" => "F",
"age" => 21,
"alert" => "bp"
),
$array3 = array(
"id" => 206,
"firstName" => "KUTAPPA.A.C",
"gender" => "M",
"age" => 31,
"alert" => "bmi"
)
);
这是我的预期输出,
$array = array (
'205' = array(
"id" => 205,
"firstName" => "NANCY GEORGE",
"gender" => "F",
"age" => 21,
"alert" => "bmi,bp"
),
'206' = array(
"id" => 206,
"firstName" => "KUTAPPA.A.C",
"gender" => "M",
"age" => 31,
"alert" => "bmi"
)
);
我正在尝试在foreach中合并数组,以创建新数组,但它无法获得预期的结果。
答案 0 :(得分:1)
这可能会对你有帮助。
$arr1 = array(
"id" => 205,
"firstName" => "NANCY GEORGE",
"gender" => "F",
"age" => 21,
"alert" => "bmi"
);
$arr2 = array(
"id" => 205,
"firstName" => "NANCY GEORGE",
"gender" => "F",
"age" => 21,
"alert" => "bp"
);
$arr3 = array(
"id" => 206,
"firstName" => "KUTAPPA.A.C",
"gender" => "M",
"age" => 31,
"alert" => "bmi"
);
$arr4 = array($arr1, $arr2, $arr3);
$n = count($arr4);
for($i=0; $i<=$n; $i++){
if($arr4[$i]['id'] == $arr4[$i+1]['id']){
$id = $arr4[$i]['id'];
foreach($arr4[$i] as $key=>$value){
if($arr4[$i][$key] <> $arr4[$i+1][$key]){
$str = array($arr4[$i][$key], $arr4[$i+1][$key]);
$str = implode(',', $str);
$arr5[$id][$key] = $str;
}
else {
$arr5[$id][$key] = $arr4[$i][$key];
}
}
$i++;
}
elseif($arr4[$i]['id'] <> $arr4[$i+1]['id']) {
$id = $arr4[$i]['id'];
$arr5[$id] = $arr4[$i];
}
}
print_r($arr5);
输出 -
Array
(
[205] => Array
(
[id] => 205
[firstName] => NANCY GEORGE
[gender] => F
[age] => 21
[alert] => bmi,bp
)
[206] => Array
(
[id] => 206
[firstName] => KUTAPPA.A.C
[gender] => M
[age] => 31
[alert] => bmi
)
)
答案 1 :(得分:1)
这应该可以解决问题,但正如其中一条评论指出的那样,如果您可以修复数据库以便为您分组,那将是理想的。
<?php
$arrays = array (
$array1 = array(
"id" => 205,
"firstName" => "NANCY GEORGE",
"gender" => "F",
"age" => 21,
"alert" => "bmi"
),
$array2 = array(
"id" => 205,
"firstName" => "NANCY GEORGE",
"gender" => "F",
"age" => 21,
"alert" => "bp"
),
$array3 = array(
"id" => 206,
"firstName" => "KUTAPPA.A.C",
"gender" => "M",
"age" => 31,
"alert" => "bmi"
)
);
$cleanArray; //???
foreach($arrays as $array){
if(!empty($cleanArray)){
foreach($cleanArray as $cleanKey => $clean){
if($array['id'] == $cleanKey){
//Just did alerts because I hope nothing else needs this...
if(!($array['alert'] == $clean['alert'])){
$cleanArray[$cleanKey]['alert'] .= ", " . $array['alert'];
}
} else {
$id = $array['id'];
unset($array['id']);
$cleanArray[$id] = $array;
}
}
} else {
$id = $array['id'];
unset($array['id']);
$cleanArray[$id] = $array;
}
}
echo"<pre>";
print_r($cleanArray);
答案 2 :(得分:0)
尝试将为你工作......
<?php
$array = array (
$array1 = array(
"id" => 205,
"firstName" => "NANCY GEORGE",
"gender" => "F",
"age" => 21,
"alert" => "bmi"
),
$array2 = array(
"id" => 205,
"firstName" => "NANCY GEORGE",
"gender" => "F",
"age" => 21,
"alert" => "bp"
),
$array3 = array(
"id" => 206,
"firstName" => "KUTAPPA.A.C",
"gender" => "M",
"age" => 31,
"alert" => "bmi"
)
);
echo "<pre>"; print_r($array);
$newArray =array();
foreach($array as $key => $val)
{
$newArray[$val['id']] = $val;
}
echo "<pre>"; print_r($newArray);
?>
输出
Array
(
[205] => Array
(
[id] => 205
[firstName] => NANCY GEORGE
[gender] => F
[age] => 21
[alert] => bp
)
[206] => Array
(
[id] => 206
[firstName] => KUTAPPA.A.C
[gender] => M
[age] => 31
[alert] => bmi
)
)