1
我有这个PHP数组合并PHP数组元素
array [
[0] => array [
name => 'a',
service => 'x1'
],
[1] => array [
name => 'a',
service => 'x2'
],
[2] => array [
name => 'a',
service => 'x3'
],
[3] => array [
name => 'b',
service => 'x1'
],
[4] => array [
name => 'b',
service => 'x3'
],
[5] => array [
name => 'b',
service => 'x5'
]
]
我想marage全部的服务中的一个元素是什么和B以及类似如下:
array [
[0] => array [
name => 'a',
service => 'x1, x2, x3'
],
[1] => array [
name => 'b',
service => 'x1, x3, x5'
]
]
php代码:
$new_services = array();
foreach($services as $service) {
if (isset($new_services[$service->name])) {
$new_services[$service->name] .= $service['name'].',';
}
else{
$new_services[$service->name] = $service['service'];
}
}
预期的结果是不一样的,我想要的。
凡在你尝试,如果两个项目具有相同的名称,你检查?你是如何期望PHP知道你想要的? – IMSoP
那么,我们这个问题在哪里? – Rizier123
结果 –