0

如何在三维数组上运行 array_map?我想在哪里“清除”最里面的数组?

它看起来像这样:

Array
(
    [1] => Array
        (
            [1] => Array
                (
                    [cat] => Hello!
                    [url] => hello
                )

            [5] => Array
                (
                    [cat] => Good job!
                    [url] => good-job
                )

    [2] => Array
        (
            [2] => Array
                (
                    [cat] => How are you?
                    [url] => how-are-you
                )

            [6] => Array
                (
                    [cat] => Running shoes
                    [url] => running-shoes
                )
        )
)

我想让它看起来像这样:

Array
(
    [1] => Array
        (
            [1] => Array
                (
                    [cat] => Hello!
                    [url] => hello
                )

            [2] => Array
                (
                    [cat] => Good job!
                    [url] => good-job
                )

    [2] => Array
        (
            [1] => Array
                (
                    [cat] => How are you?
                    [url] => how-are-you
                )

            [2] => Array
                (
                    [cat] => Running shoes
                    [url] => running-shoes
                )
        )

)

这个解决方案在php中重置数组元素的键?如果我没记错的话,“just”适用于两个维度阵列。

4

1 回答 1

0

您可以编写一个简短的函数来使用array_map:

function mappingfunction($array){
    $remappedarray = array();
    foreach($array as $layer){
        $remappedarray[] = array_map('array_values', $array);
    }

    return $remappedarray;
}

如果要保留密钥:

function mappingfunction($array){
        $remappedarray = array();
        foreach($array as $key => $layer){
            $remappedarray[$key] = array_map('array_values', $array);
        }

        return $remappedarray;
    }

未经测试,但应该为您指明正确的方向。

于 2013-11-07T16:15:34.990 回答