PHP通过替换相同的键并添加新的来合并关联数组

时间:2014-08-04 08:51:39

标签: php arrays

我有一个像这样结构化的可变大小数组(categories只是data中的一个键):

print_r($json[123]["data"]["categories"]);
array(
  array(
    'id' => '2',
    'description' => 'Single-player'
  ),
  array(
    'id' => '1',
    'description' => 'Multi-player'
  ),
  array(
    'id' => '9',
    'description' => 'Co-op'
  ),
  array(
    'id' => '22',
    'description' => 'Steam Achievements'
  ),
  array(
    'id' => '28',
    'description' => 'Full controller support'
  )
)

print_r($json[456]["data"]["categories"]);
array(
  array(
    'id' => '21',
    'description' => 'Downloadable Content'
  ),
  array(
    'id' => '1',
    'description' => 'Multi-player'
  )
)

现在,我想合并这些子数组(它们可以是可变数)并添加和替换所有键。我已经尝试过array_merge,但它会替换密钥而不添加新密钥 在这种情况下,我需要获取此数组:

print_r($merged["data"]["categories"]);
array(
  array(
    'id' => '2',
    'description' => 'Single-player'
  ),
  array(
    'id' => '1',
    'description' => 'Multi-player'
  ),
  array(
    'id' => '9',
    'description' => 'Co-op'
  ),
  array(
    'id' => '22',
    'description' => 'Steam Achievements'
  ),
  array(
    'id' => '28',
    'description' => 'Full controller support'
  ),
  array(
    'id' => '21',
    'description' => 'Downloadable Content'
  )
)

任何帮助?

修改
我想我没有表现得很好。 $json[$id]["data"]有多个我要合并的密钥(categories只是一个例子)。 $json[$id]个键的数量也是变量的 的 EDIT2:
数组可以具有重复值,并且键的深度可以是可变的。我需要得到像array_merge_recursive()这样的东西,但是替换了相同的值 的 EDIT3:
这是当前的数组。 http://pastebin.com/7x7KaAVM我需要合并所有具有子数组的键

2 个答案:

答案 0 :(得分:0)

试试这个。为了概括,我添加了一些更多的数组。

<?php
    $merged_array = array();
    $final_array = array();
    $json[123]["data"]["categories"] = array(
                                            array(
                                              'id' => '2',
                                              'description' => 'Single-player'
                                            ),
                                            array(
                                              'id' => '1',
                                              'description' => 'Multi-player'
                                            ),
                                            array(
                                              'id' => '9',
                                              'description' => 'Co-op'
                                            ),
                                            array(
                                              'id' => '22',
                                              'description' => 'Steam Achievements'
                                            ),
                                            array(
                                              'id' => '28',
                                              'description' => 'Full controller support'
                                            )
                                          );
    $json[456]["data"]["categories"] = array(
                                            array(
                                              'id' => '21',
                                              'description' => 'Downloadable Content'
                                            )
                                          );
    $json[786]["data"]["categories"] = array(
                                            array(
                                              'id' => '31',
                                              'description' => 'Downloadable Content'
                                            )
                                          );
    $json[058]["data"]["categories"] = array(
                                            array(
                                              'id' => '41',
                                              'description' => 'Downloadable Content'
                                            )
                                          );

    foreach($json as $key=>$value){
        array_push($merged_array,$json[$key]["data"]["categories"]);      
    }

    foreach($merged_array as $value){
        foreach($value as $val){
            array_push($final_array,$val);      
        }
    }
    print_r($final_array);

?>

<强> RESULT

Array
(
    [0] => Array
        (
            [id] => 2
            [description] => Single-player
        )

    [1] => Array
        (
            [id] => 1
            [description] => Multi-player
        )

    [2] => Array
        (
            [id] => 9
            [description] => Co-op
        )

    [3] => Array
        (
            [id] => 22
            [description] => Steam Achievements
        )

    [4] => Array
        (
            [id] => 28
            [description] => Full controller support
        )

    [5] => Array
        (
            [id] => 21
            [description] => Downloadable Content
        )

    [6] => Array
        (
            [id] => 31
            [description] => Downloadable Content
        )

    [7] => Array
        (
            [id] => 41
            [description] => Downloadable Content
        )

)

答案 1 :(得分:0)

请尝试以下代码:

$json = array(
'123' => array('data' => array('categories' => array(
        array(
            'id' => '2',
            'description' => 'Single-player'
        ),
        array(
            'id' => '1',
            'description' => 'Multi-player'
        ),
        array(
            'id' => '9',
            'description' => 'Co-op'
        ),
        array(
            'id' => '22',
            'description' => 'Steam Achievements'
        ),
        array(
            'id' => '28',
            'description' => 'Full controller support'
        )
    ))
              ),
'456' => array('data' => array('categories' => array(
        array(
            'id' => '21',
            'description' => 'Downloadable Content'
            )
        ))
       ),
);

//print_r($json);

$merged = array();
foreach($json as $j1)
{
    foreach($j1 as $j2)
    {
        foreach($j2 as $key => $j3)
        {
            foreach($j3 as $j4)
            {
                $merged[$key][] = $j4;
            }
        }
    }
}

print_r($merged);


结果:

Array
(
    [categories] => Array
        (
            [0] => Array
                (
                    [id] => 2
                    [description] => Single-player
                )

            [1] => Array
                (
                    [id] => 1
                    [description] => Multi-player
                )

            [2] => Array
                (
                    [id] => 9
                    [description] => Co-op
                )

            [3] => Array
                (
                    [id] => 22
                    [description] => Steam Achievements
                )

            [4] => Array
                (
                    [id] => 28
                    [description] => Full controller support
                )

            [5] => Array
                (
                    [id] => 21
                    [description] => Downloadable Content
                )

        )

)


演示:

http://3v4l.org/X61bE#v430