从一个数组中找到相同的数组值并将其值合并到新的数组组中
Find Same Array value from one single array and merge its value into new array group wise
我有一个多维数组,我想从该数组创建一个新数组,该数组来自第一个数组的相同 id1,并希望将所有匹配的子数组值及其元素
放入其内部数组中
下面是我的数组:
Array
(
[0] => Array
(
[id1] => 109891
[id2] => 67
)
[1] => Array
(
[id1] => 828393
[id2] => 67
)
[2] => Array
(
[id1] => 828393
[id2] => 68
)
[3] => Array
(
[id1] => 816714
[id2] => 70
)
[4] => Array
(
[id1] => 816714
[id2] => 67
)
[5] => Array
(
[id1] => 816714
[id2] => 68
)
)
我想要这样的输出:
Array
(
[109891] => Array
(
[0] => Array
(
[id2] => 67
)
)
[828393] => Array
(
[0] => Array
(
[id2] => 67
)
[1] => Array
(
[id2] => 68
)
)
[816714] => Array
(
[0] => Array
(
[id2] => 70
)
[1] => Array
(
[id2] => 67
)
[2] => Array
(
[id2] => 68
)
)
)
任何帮助将不胜感激
使用以下测试代码:
$i = 0;
$sepr = 0;
$arr = array();
foreach($your_array as $your_reslut_array) {
$k = $i; $k = ($k > 0 ? $k-1 : 0);
foreach($your_reslut_array as $key => $value) {
if($key != "ID" && $value != $your_reslut_array[$k][$key]) {
$sepr++;
}
}
$arr[$sepr][] = $car;
$i++;
}
print_r($arr);
原始数组:
$array = array(
array('id1' => '109891', 'id2' => '67'),
array('id1' => '828393', 'id2' => '67'),
array('id1' => '828393', 'id2' => '68'),
array('id1' => '816714', 'id2' => '70'),
);
使用这个foreach:
$new = array();
foreach($array as $element) {
$new[$element['id1']][] = array('id2' => $element['id2']);
}
或者函数中的这个:
function editMyArray($array) {
$newArray = array();
foreach($array as $element) {
$newArray[$element['id1']][] = array('id2' => $element['id2']);
}
return $newArray;
}
输出:
echo "<pre>";
print_r($new);
echo "</pre>";
Array
(
[109891] => Array
(
[0] => Array
(
[id2] => 67
)
)
[828393] => Array
(
[0] => Array
(
[id2] => 67
)
[1] => Array
(
[id2] => 68
)
)
[816714] => Array
(
[0] => Array
(
[id2] => 70
)
)
)
应该这样做:
$output = array();
foreach($input as $r) {
$id1 = $r['id1'];
if(!isset($output[$id1])) {
$output[$id1] = array();
}
$output[$id1][] = array('id2' => $r['id2']);
}
print_r($output);
我有一个多维数组,我想从该数组创建一个新数组,该数组来自第一个数组的相同 id1,并希望将所有匹配的子数组值及其元素
放入其内部数组中下面是我的数组:
Array
(
[0] => Array
(
[id1] => 109891
[id2] => 67
)
[1] => Array
(
[id1] => 828393
[id2] => 67
)
[2] => Array
(
[id1] => 828393
[id2] => 68
)
[3] => Array
(
[id1] => 816714
[id2] => 70
)
[4] => Array
(
[id1] => 816714
[id2] => 67
)
[5] => Array
(
[id1] => 816714
[id2] => 68
)
)
我想要这样的输出:
Array
(
[109891] => Array
(
[0] => Array
(
[id2] => 67
)
)
[828393] => Array
(
[0] => Array
(
[id2] => 67
)
[1] => Array
(
[id2] => 68
)
)
[816714] => Array
(
[0] => Array
(
[id2] => 70
)
[1] => Array
(
[id2] => 67
)
[2] => Array
(
[id2] => 68
)
)
)
任何帮助将不胜感激
使用以下测试代码:
$i = 0;
$sepr = 0;
$arr = array();
foreach($your_array as $your_reslut_array) {
$k = $i; $k = ($k > 0 ? $k-1 : 0);
foreach($your_reslut_array as $key => $value) {
if($key != "ID" && $value != $your_reslut_array[$k][$key]) {
$sepr++;
}
}
$arr[$sepr][] = $car;
$i++;
}
print_r($arr);
原始数组:
$array = array(
array('id1' => '109891', 'id2' => '67'),
array('id1' => '828393', 'id2' => '67'),
array('id1' => '828393', 'id2' => '68'),
array('id1' => '816714', 'id2' => '70'),
);
使用这个foreach:
$new = array();
foreach($array as $element) {
$new[$element['id1']][] = array('id2' => $element['id2']);
}
或者函数中的这个:
function editMyArray($array) {
$newArray = array();
foreach($array as $element) {
$newArray[$element['id1']][] = array('id2' => $element['id2']);
}
return $newArray;
}
输出:
echo "<pre>";
print_r($new);
echo "</pre>";
Array
(
[109891] => Array
(
[0] => Array
(
[id2] => 67
)
)
[828393] => Array
(
[0] => Array
(
[id2] => 67
)
[1] => Array
(
[id2] => 68
)
)
[816714] => Array
(
[0] => Array
(
[id2] => 70
)
)
)
应该这样做:
$output = array();
foreach($input as $r) {
$id1 = $r['id1'];
if(!isset($output[$id1])) {
$output[$id1] = array();
}
$output[$id1][] = array('id2' => $r['id2']);
}
print_r($output);