多个数组在php中与concat合并

时间:2016-09-21 11:59:17

标签: php arrays

如果数组标题是同名,我需要使用值concat进行数组合并。我的阵列打印是 - >

Array
(
    [0] => Array
        (
            [id] => 7867867
            [title] => Title1
        )

    [1] => Array
        (
            [id] => 3452342
            [title] => Title2
        )

    [2] => Array
        (
            [id] => 1231233
            [title] => Title2
        )

    [3] => Array
        (
            [id] => 5867867
            [title] => Title1
        )

    [4] => Array
        (
            [id] => 7867777
            [title] => Title1
        )

)

我希望这种格式,如果title与一个数组中的数组值相同,而其他数组正在删除。

喜欢那样 - >

Array
(
    [0] => Array
        (
            [id] => 7867867,5867867,7867777
            [title] => Title1,Title1,Title1
        )

    [1] => Array
        (
            [id] => 3452342,1231233
            [title] => Title2,Title2
        )

)

如果您知道如何解决它请帮助我! 感谢

1 个答案:

答案 0 :(得分:1)

试试这个,

foreach($array as $val)
{       
    $titlearray[] = $val['title'];
}
$titlearray = (array_unique($titlearray));

//print_r($titlearray);

foreach($array as $val)
{
    $key = array_search($val['title'], $titlearray); 
    $newarray[$key]['id'][] = $val['id'];
    $newarray[$key]['title'][] = $val['title']; 
}

<强> DEMO