PHP 数组将具有相同字符串键的值组合在一起
PHP Array combine values with same string key
我遇到了以下似乎很常见但无法确定哪个数组函数适用于以下格式的问题:
(已尝试 array_merge
、 array_merge_recursive
、 array_combine
、array_splice
但没有按预期工作。)
Array
(
[0] => Array
(
[r_id] => 11
[r_sid] => RC
[d_id] => 2
)
[1] => Array
(
[r_id] => 7
[r_sid] => RC
[c_id] => 51
)
[2] => Array
(
[r_id] => 6
[r_sid] => JN
[c_id] => 52
)
[3] => Array
(
[r_id] => 7
[r_sid] => JN
[c_id] => 51
)
[4] => Array
(
[r_id] => 7
[r_sid] => LG
[c_id] => 51
)
[5] => Array
(
[r_id] => 7
[r_sid] => BN
[c_id] => 51
)
[6] => Array
(
[r_id] => 6
[r_sid] => IVS
[c_id] => 52
)
[7] => Array
(
[r_id] => 7
[r_sid] => IVS
[c_id] => 51
)
)
现在,我需要通过常用的 r_sid
和 c_id
键来合并这个数组值;唯一的特殊情况是,如果有一个 d_id
键而不是 c_id
,那么我们 merge/combine 数组中具有类似 r_sid
的任何值。
最终的解决方案需要看起来像这样,如果这样更容易理解的话:
Array
(
[0] => Array
(
[r_id] => 11,7
[r_sid] => RC
[d_id] => 2
[c_id] => 51
)
[1] => Array
(
[r_id] => 6,7
[r_sid] => JN, IVS
[c_id] => 52,51
)
)
与任何人都不匹配的r_sid
值需要被丢弃。
感谢任何帮助。非常感谢!
这是我对如何合并数组的最佳解释的一段代码:
// filter out r_sid values which only occur once
$sid_values = array_count_values(array_column($array, 'r_sid'));
$array = array_filter($array, function ($a) use ($sid_values) { return $sid_values[$a['r_sid']] > 1; });
// combine arrays on r_sid values
$result = array();
foreach ($array as $arr) {
$sid = $arr['r_sid'];
// push data to arrays
$result[$sid]['r_sid'] = $sid;
$result[$sid]['r_id'][] = $arr['r_id'];
if (isset($arr['c_id'])) $result[$sid]['c_id'][] = $arr['c_id'];
if (isset($arr['d_id'])) $result[$sid]['d_id'][] = $arr['d_id'];
}
// combine all the c_id values into a string
array_walk($result, function (&$a) {
if (isset($a['c_id'])) $a['c_id'] = implode(',', $a['c_id']);
});
// now combine any r_sid values that have the same set of c_id values
$output = array();
foreach ($result as $res) {
$cid = $res['c_id'];
// push data to arrays
$output[$cid]['c_id'] = $cid;
$output[$cid]['r_sid'] = array_merge($output[$cid]['r_sid'] ?? array(), array($res['r_sid']));
$output[$cid]['r_id'] = array_merge($output[$cid]['r_id'] ?? array(), $res['r_id']);
if (isset($res['d_id'])) $output[$cid]['d_id'] = array_merge($output[$cid]['d_id'] ?? array(), $res['d_id']);
}
// combine the r_sid, r_id and d_id values into a string
array_walk($output, function (&$a) {
$a['r_sid'] = implode(',', $a['r_sid']);
$a['r_id'] = implode(',', array_unique($a['r_id']));
if (isset($a['d_id'])) $a['d_id'] = implode(',', $a['d_id']);
});
// remove the associative keys
$output = array_values($output);
输出:
Array
(
[0] => Array
(
[c_id] => 51
[r_sid] => RC
[r_id] => 11,7
[d_id] => 2
)
[1] => Array
(
[c_id] => 52,51
[r_sid] => JN,IVS
[r_id] => 6,7
)
)
我遇到了以下似乎很常见但无法确定哪个数组函数适用于以下格式的问题:
(已尝试 array_merge
、 array_merge_recursive
、 array_combine
、array_splice
但没有按预期工作。)
Array
(
[0] => Array
(
[r_id] => 11
[r_sid] => RC
[d_id] => 2
)
[1] => Array
(
[r_id] => 7
[r_sid] => RC
[c_id] => 51
)
[2] => Array
(
[r_id] => 6
[r_sid] => JN
[c_id] => 52
)
[3] => Array
(
[r_id] => 7
[r_sid] => JN
[c_id] => 51
)
[4] => Array
(
[r_id] => 7
[r_sid] => LG
[c_id] => 51
)
[5] => Array
(
[r_id] => 7
[r_sid] => BN
[c_id] => 51
)
[6] => Array
(
[r_id] => 6
[r_sid] => IVS
[c_id] => 52
)
[7] => Array
(
[r_id] => 7
[r_sid] => IVS
[c_id] => 51
)
)
现在,我需要通过常用的 r_sid
和 c_id
键来合并这个数组值;唯一的特殊情况是,如果有一个 d_id
键而不是 c_id
,那么我们 merge/combine 数组中具有类似 r_sid
的任何值。
最终的解决方案需要看起来像这样,如果这样更容易理解的话:
Array
(
[0] => Array
(
[r_id] => 11,7
[r_sid] => RC
[d_id] => 2
[c_id] => 51
)
[1] => Array
(
[r_id] => 6,7
[r_sid] => JN, IVS
[c_id] => 52,51
)
)
与任何人都不匹配的r_sid
值需要被丢弃。
感谢任何帮助。非常感谢!
这是我对如何合并数组的最佳解释的一段代码:
// filter out r_sid values which only occur once
$sid_values = array_count_values(array_column($array, 'r_sid'));
$array = array_filter($array, function ($a) use ($sid_values) { return $sid_values[$a['r_sid']] > 1; });
// combine arrays on r_sid values
$result = array();
foreach ($array as $arr) {
$sid = $arr['r_sid'];
// push data to arrays
$result[$sid]['r_sid'] = $sid;
$result[$sid]['r_id'][] = $arr['r_id'];
if (isset($arr['c_id'])) $result[$sid]['c_id'][] = $arr['c_id'];
if (isset($arr['d_id'])) $result[$sid]['d_id'][] = $arr['d_id'];
}
// combine all the c_id values into a string
array_walk($result, function (&$a) {
if (isset($a['c_id'])) $a['c_id'] = implode(',', $a['c_id']);
});
// now combine any r_sid values that have the same set of c_id values
$output = array();
foreach ($result as $res) {
$cid = $res['c_id'];
// push data to arrays
$output[$cid]['c_id'] = $cid;
$output[$cid]['r_sid'] = array_merge($output[$cid]['r_sid'] ?? array(), array($res['r_sid']));
$output[$cid]['r_id'] = array_merge($output[$cid]['r_id'] ?? array(), $res['r_id']);
if (isset($res['d_id'])) $output[$cid]['d_id'] = array_merge($output[$cid]['d_id'] ?? array(), $res['d_id']);
}
// combine the r_sid, r_id and d_id values into a string
array_walk($output, function (&$a) {
$a['r_sid'] = implode(',', $a['r_sid']);
$a['r_id'] = implode(',', array_unique($a['r_id']));
if (isset($a['d_id'])) $a['d_id'] = implode(',', $a['d_id']);
});
// remove the associative keys
$output = array_values($output);
输出:
Array
(
[0] => Array
(
[c_id] => 51
[r_sid] => RC
[r_id] => 11,7
[d_id] => 2
)
[1] => Array
(
[c_id] => 52,51
[r_sid] => JN,IVS
[r_id] => 6,7
)
)