展平嵌套多维数组

时间:2020-04-08 12:57:02

标签: php multidimensional-array

我有一个从JSON解码的多维数组,其中包括嵌套数组:

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [ID] => 1
                    [post_author] => 2
                    [post_date] => 2020-04-07 09:36:41
                    [post_title] => Hello world!
                )
        )
    [2] => Array
        (
            [0] => Array
                (
                    [ID] => 1
                    [post_author] => 1
                    [post_date] => 2020-04-07 09:37:12
                    [post_title] => Hello world!
                )
        )
    [3] => Array
        (
            [0] => Array
                (
                    [ID] => 1
                    [post_author] => 1
                    [post_date] => 2020-04-07 09:37:40
                    [post_title] => Hello world!
                )

            [1] => Array
                (
                    [ID] => 4
                    [post_author] => 1
                    [post_date] => 2019-04-08 11:59:27
                    [post_title] => uncategorised post
                )
        )
)

我想遍历数组,将它们转换为对象,然后将它们添加到新的数组中,如下所示:

[
    {
        id: 1
        post_author: 2
        post_date: 2020-04-07 09:36:41
        post_title: Hello world!
    },{
        id: 1
        post_author: 1
        post_date: 2020-04-07 09:37:12
        post_title: Hello world!
    },{
        id: 1
        post_author: 1
        post_date 2020-04-07 09:37:40
        post_title: Hello world!
    },{
        id: 4
        post_author: 1
        post_date: 2019-04-08 11:59:27
        post_title: uncategorised post
    }
]

这可能吗?

我已经尝试了很多方法,但是还没有找到一种方法。谁能帮忙吗?

0 个答案:

没有答案