array_map三维数组

时间:2013-11-07 16:06:05

标签: php arrays sorting array-map

如何在三维数组上运行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
                )
        )

如果我没有错,这个解决方案Reset keys of array elements in php?“只是”适用于两个数据阵列。

1 个答案:

答案 0 :(得分: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;
    }

未经测试,但应指向正确的方向。