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
結果 –