PHP:从while循环+排序合并两个数组

时间:2014-02-20 20:47:27

标签: php arrays merge while-loop

在Wordpress中,每个WP_query都会返回帖子。 (目前情况我有2个帖子) 每个帖子都包含一些数组。结果我得到两个数组,我需要合并它们,以便我可以稍后对它们进行排序。 所以目前如果我打印:

echo "<pre>";
print_r($products_selected);
echo "</pre>";

我明白了:

Array
(

    [0] => Array
        (
            [0] => http://www.page1.com
            [1] => Product 1
        )

    [1] => Array
        (
            [0] => http://www.page2.com
            [1] => Product 2

        )
)

数组2:

Array
(

    [0] => Array
        (
            [0] => http://www.page3.com
            [1] => Product 3
        )

    [1] => Array
        (
            [0] => http://www.page4.com
            [1] => Product 4

        )
    [2] => Array
        (
            [0] => http://www.page5.com
            [1] => Product 5

        )

)

如何合并它们,以便按产品名称([1])

进行排序

示例:

Array
(

    [0] => Array
        (
            [0] => http://www.page1.com
            [1] => Product 1
        )

    [1] => Array
        (
            [0] => http://www.page2.com
            [1] => Product 2

        )

    [2] => Array
        (
            [0] => http://www.page3.com
            [1] => Product 3
        )

    [3] => Array
        (
            [0] => http://www.page4.com
            [1] => Product 4

        )
    [4] => Array
        (
            [0] => http://www.page5.com
            [1] => Product 5

        )

)

1 个答案:

答案 0 :(得分:0)

您可以使用array_merge_recursive($ array1, $ array2)

获得此确切结果