0

我在下面有这个数组,并希望像这样组织它:类别 22 包含 783,784,785。类别 11 包含 783,784,785 等等。我有点卡住了......所以下面是源代码,下面是我想要将数组转换为的内容。

Array
(
    [783] = Array
        (
            [categories] = Array
                (
                    [0] = 22
                    [1] = 11
                    [2] = 12
                )

        )

    [784] = Array
        (
            [categories] = Array
                (
                    [0] = 22
                    [1] = 11
                    [2] = 12
                )

        )

    [785] = Array
        (
            [categories] = Array
                (
                    [0] = 22
                    [1] = 11
                    [2] = 12
                )

        )

)

想将数组转换为此。

Array
    (
        [22] = Array
            (
                [0] => 783
                [1] => 784   
                [2] => 785
            )

        [11] = Array
            (
                [0] => 783
                [1] => 784   
                [2] => 785
            )

        [12] = Array
            (
                [0] => 783
                [1] => 784   
                [2] => 785
            )
    )
4

1 回答 1

2

So just iterate over the original array and assemble the result; something like this:

$categories = array();

foreach ($data as $itemId => $itemData) {
        foreach ($itemData['categories'] as $categoryId) {
                $categories[$categoryId][] = $itemId;
        }
}
于 2013-05-27T02:08:55.437 回答