在 PHP 中,对于关联数组中的特定键,找到相同的键并将值组合成单个字符串变量
In PHP, for a specific key in an associative array, find identical keys and combine values into a single string variable
我在 SO 上找到了很多类似问题的参考资料,但与我正在尝试做的事情相去甚远。我有这个数组输出:
0: {id: 1000012, kp_uid: 100000570, assigned_uid: 'tim.hughes@sampleco.com', full_name: 'Tim Hughes'}
1: {id: 1000013, kp_uid: 100000570, assigned_uid: 'brad.slater@sampleco.com', full_name: 'Brad Slater'}
2: {id: 1000014, kp_uid: 100000570, assigned_uid: 'karen.tevis@sampleco.com', full_name: 'Karen Tevis'}
3: {id: 1000015, kp_uid: 100000597, assigned_uid: 'karen.tevis@sampleco.com', full_name: 'Karen Tevis'}
我现在想使用 'kp_uid' 来定位所有常用值,然后将每个键的 'full_name' 值组合成一个字符串(用逗号分隔)。上述示例的预期结果将是:
0: {kp_uid: 100000570, full_name: 'Tim Hughes, Brad Slater, Karen Tevis'}
1: {kp_uid: 100000597, full_name: 'Karen Tevis'}
我尝试了很多从 SO 收集的想法,这是最接近的,但我无法分离出唯一的密钥:
unset($kp_assign['id']);
unset($kp_assign['assigned_uid']);
$result = array();
foreach ($kp_assign as $arr) {
foreach($arr as $key => $val) {
$result[$key][] = $val;
}
}
return $result;
部分结果显示所有四个名字:
full_name: Array(4)
0: "Tim Hughes"
1: "Brad Slater"
2: "Karen Tevis"
3: "Karen Tevis"
如有任何指示,我们将不胜感激。谢谢。
尝试这样的事情
unset($kp_assign['id']);
unset($kp_assign['assigned_uid']);
$result = array();
foreach ($kp_assign as $arr) {
foreach($arr as $key => $val) {
$result[$key][] = $val;
}
}
// loop through results, using & to reference original value in array
// you will need to change this to suit the contents of your results array, it is not clear from the example what the keys within the results array will be
foreach ($results as $key => &$val) {
$val = implode(", ", $val); // implode the names, with comma separator
}
return $result;
$items = [
['id' => 1000012, 'kp_uid' => 100000570, 'assigned_uid' => 'tim.hughes@sampleco.com', 'full_name' => 'Tim Hughes'],
['id' => 1000013, 'kp_uid' => 100000570, 'assigned_uid' => 'brad.slater@sampleco.com', 'full_name' => 'Brad Slater'],
['id' => 1000014, 'kp_uid' => 100000570, 'assigned_uid' => 'karen.tevis@sampleco.com', 'full_name' => 'Karen Tevis'],
['id' => 1000015, 'kp_uid' => 100000597, 'assigned_uid' => 'karen.tevis@sampleco.com', 'full_name' => 'Karen Tevis']
];
$grouped = [];
// group items by kp_uid
foreach ($items as $item) {
$grouped[$item['kp_uid']][] = $item;
}
function mapNamesCallback($item)
{
return $item['full_name'];
}
// iterate over groups and return a single item
// in form of kp_uid => x, full_names => 'name, name2 etc.'
$result = array_map(function ($group, $kpUid) {
return ['kp_uid' => $kpUid, 'full_name' => implode(', ', array_map('mapNamesCallback', $group))];
}, $grouped, array_keys($grouped));
这是 returns 想要的结果。
为了清楚起见,我在代码中留下了一些注释。
我在 SO 上找到了很多类似问题的参考资料,但与我正在尝试做的事情相去甚远。我有这个数组输出:
0: {id: 1000012, kp_uid: 100000570, assigned_uid: 'tim.hughes@sampleco.com', full_name: 'Tim Hughes'}
1: {id: 1000013, kp_uid: 100000570, assigned_uid: 'brad.slater@sampleco.com', full_name: 'Brad Slater'}
2: {id: 1000014, kp_uid: 100000570, assigned_uid: 'karen.tevis@sampleco.com', full_name: 'Karen Tevis'}
3: {id: 1000015, kp_uid: 100000597, assigned_uid: 'karen.tevis@sampleco.com', full_name: 'Karen Tevis'}
我现在想使用 'kp_uid' 来定位所有常用值,然后将每个键的 'full_name' 值组合成一个字符串(用逗号分隔)。上述示例的预期结果将是:
0: {kp_uid: 100000570, full_name: 'Tim Hughes, Brad Slater, Karen Tevis'}
1: {kp_uid: 100000597, full_name: 'Karen Tevis'}
我尝试了很多从 SO 收集的想法,这是最接近的,但我无法分离出唯一的密钥:
unset($kp_assign['id']);
unset($kp_assign['assigned_uid']);
$result = array();
foreach ($kp_assign as $arr) {
foreach($arr as $key => $val) {
$result[$key][] = $val;
}
}
return $result;
部分结果显示所有四个名字:
full_name: Array(4)
0: "Tim Hughes"
1: "Brad Slater"
2: "Karen Tevis"
3: "Karen Tevis"
如有任何指示,我们将不胜感激。谢谢。
尝试这样的事情
unset($kp_assign['id']);
unset($kp_assign['assigned_uid']);
$result = array();
foreach ($kp_assign as $arr) {
foreach($arr as $key => $val) {
$result[$key][] = $val;
}
}
// loop through results, using & to reference original value in array
// you will need to change this to suit the contents of your results array, it is not clear from the example what the keys within the results array will be
foreach ($results as $key => &$val) {
$val = implode(", ", $val); // implode the names, with comma separator
}
return $result;
$items = [
['id' => 1000012, 'kp_uid' => 100000570, 'assigned_uid' => 'tim.hughes@sampleco.com', 'full_name' => 'Tim Hughes'],
['id' => 1000013, 'kp_uid' => 100000570, 'assigned_uid' => 'brad.slater@sampleco.com', 'full_name' => 'Brad Slater'],
['id' => 1000014, 'kp_uid' => 100000570, 'assigned_uid' => 'karen.tevis@sampleco.com', 'full_name' => 'Karen Tevis'],
['id' => 1000015, 'kp_uid' => 100000597, 'assigned_uid' => 'karen.tevis@sampleco.com', 'full_name' => 'Karen Tevis']
];
$grouped = [];
// group items by kp_uid
foreach ($items as $item) {
$grouped[$item['kp_uid']][] = $item;
}
function mapNamesCallback($item)
{
return $item['full_name'];
}
// iterate over groups and return a single item
// in form of kp_uid => x, full_names => 'name, name2 etc.'
$result = array_map(function ($group, $kpUid) {
return ['kp_uid' => $kpUid, 'full_name' => implode(', ', array_map('mapNamesCallback', $group))];
}, $grouped, array_keys($grouped));
这是 returns 想要的结果。 为了清楚起见,我在代码中留下了一些注释。