在PHP中合并数组

时间:2013-12-04 16:50:20

标签: php arrays

我有一堆1到10个结果的数组,我需要找到一种方法来合并这两个,我是php的新手,所以我可能需要一些帮助

Array
(

[906] => Array
    (
        [id] => 906
        [form] => DJ Form
        [date] => 2013-12-04 16:41:07
        [timestamp] => 1386193267
        [email] => 
        [data] => Array
            (
                [#1] => kaskade
                [#2] => armin van buren
                [#3] => kap slap
                [#4] => avicii
                [#5] => calvin harris
                [#6] => dada life
                [#7] => nicky romero
                [#8] => swanky tunes
                [#9] => eric prydz
                [#10] => bingo players
            )

    )

[905] => Array
    (
        [id] => 905
        [form] => DJ Form
        [date] => 2013-12-04 16:39:52
        [timestamp] => 1386193192
        [email] => 
        [data] => Array
            (
                [#1] => steve aoki
                [#2] => steve aoki
                [#3] => steve aoki
                [#4] => steve aoki
                [#5] => steve aoki
                [#6] => steve aoki
                [#7] => steve aoki
                [#8] => steve aoki
                [#9] => steve aoki
                [#10] => steve aoki
            )

    )

我希望能够将这些组合起来得到一个像这样的新数组:

[data] => Array
            (
                [#1] => steve aoki
                [#2] => steve aoki
                [#3] => steve aoki
                [#4] => steve aoki
                [#5] => steve aoki
                [#6] => steve aoki
                [#7] => steve aoki
                [#8] => steve aoki
                [#9] => steve aoki
                [#10] => steve aoki
                [#1] => kaskade
                [#2] => armin van buren
                [#3] => kap slap
                [#4] => avicii
                [#5] => calvin harris
                [#6] => dada life
                [#7] => nicky romero
                [#8] => swanky tunes
                [#9] => eric prydz
                [#10] => bingo players
            )

我还需要剥离所有空格并更改“&”之类的内容到“和”,有没有人知道这样做的好方法我需要一些帮助!请帮助我!

2 个答案:

答案 0 :(得分:0)

我认为这可行:

$result = array()
foreach($inputs as $input){
  $result = array_merge($result, array_values($input['data']));
}

$ result = str_replace('&','和',$ result);

答案 1 :(得分:0)

Lajos Veres在一个实际例子中回答:

<?php
// The result array containing one or more results
$results = array(
    906 => array(
        'id' => 906,
        'data' => array(
            0 => 'One',
            1 => 'Two',
            2 => 'Three'
        )
    ),
    907 => array(
        'id' => 907,
        'data' => array(
            0 => 'Four',
            1 => 'Five',
            2 => 'Six'
        )
    ),
);

echo "<pre>".print_r($results, true)."</pre>";

上面会输出:

Array
(
    [906] => Array
        (
            [id] => 906
            [data] => Array
                (
                    [0] => One
                    [1] => Two
                    [2] => Three
                )

        )

    [907] => Array
        (
            [id] => 907
            [data] => Array
                (
                    [0] => Four
                    [1] => Five
                    [2] => Six
                )

        )

)

Lajos Veres的代码:

// Create a temporary array
$tmp = array();

// Loop through the results
foreach ($results as $array)
{
    // Merge the current $array['data'] with the $tmp array
    $tmp = array_merge($tmp, array_values($array['data']));
}

echo "<pre>".print_r($tmp, true)."</pre>";

以上将输出:

Array
(
    [0] => One
    [1] => Two
    [2] => Three
    [3] => Four
    [4] => Five
    [5] => Six
)

因此,每个结果的['data']键中包含的任何内容都将合并为一个大数组。