使用foreach时,将数组元素向右移动一级(向下一级)

时间:2019-01-07 17:12:50

标签: php arrays multidimensional-array

很抱歉,如果有一个明显的答案,但是我已经尝试了数小时来搜索该解决方案并尝试了各种方法。基本上,我试图将多维数组中的数组向右移动一级。输出数组将被馈送到仅接受所需格式的多维数组的程序。

我们有($ result):

Array (
    [0] => Array (
        [id] => 1
        [data] => AAA
    )
    [1] => Array (
        [id] => 2
        [data] => BBB
    )
    [2] => Array (
        [id] => 3
        [data] => CCC
    )
)

我们要在多维数组之间插入一个新数组[test]。所需结果:

Array (
    [0] => Array (
        [test] => Array (
            [id] => 1
            [data] => AAA
        )
    )
    [1] => Array (
        [test] => Array (
            [id] => 2
            [data] => BBB
        )
    )
    [2] => Array (
        [test] => Array (
            [id] => 3
            [data] => CCC
        )
    )
)

到目前为止我所拥有的:

foreach ($result as $key => $value) {
    $newarray[] = array($key => $value)
};

return $newarray;

不幸的是,以上方法在我们想要的位置插入了一个增量索引而不是[test]。

有人可以帮忙吗?

4 个答案:

答案 0 :(得分:3)

您可以使用当前键在同一数组中使用循环和索引。然后使用新数组更新值,其中使用test作为键,并使用$v作为值。

$arrays = [
    0 => [
        "id" => 1,
        "data" => "AAA"
    ],
    1 => [
        "id" => 2,
        "data" => "BBB"
    ],
    2 => [
        "id" => 3,
        "data" => "CCC"
    ],
];


foreach ($arrays as $k => $v) {
    $arrays[$k] = ['test' => $v];
};

print_r($arrays);

结果

Array
(
    [0] => Array
        (
            [test] => Array
                (
                    [id] => 1
                    [data] => AAA
                )

        )

    [1] => Array
        (
            [test] => Array
                (
                    [id] => 2
                    [data] => BBB
                )

        )

    [2] => Array
        (
            [test] => Array
                (
                    [id] => 3
                    [data] => CCC
                )

        )

)

Demo

答案 1 :(得分:0)

尝试一下:

foreach ($result as $key => $value) {
    $newarray[$key] = ['test' => $value];
};

答案 2 :(得分:0)

这将是一种可行且灵活的方法:

<?php
$data = [
    [
        'id' => 1,
        'data' => "AAA"
    ],
    [
        'id' => 2,
        'data' => "BBB"
    ],
    [
        'id' => 3,
        'data' => "CCC"
    ]
];

array_walk($data, function(&$entry) {
    $entry = [
        'test' => $entry
    ];
});

print_r($data);

输出显然是:

Array
(
    [0] => Array
        (
            [test] => Array
                (
                    [id] => 1
                    [data] => AAA
                )

        )

    [1] => Array
        (
            [test] => Array
                (
                    [id] => 2
                    [data] => BBB
                )

        )

    [2] => Array
        (
            [test] => Array
                (
                    [id] => 3
                    [data] => CCC
                )

        )
)

答案 3 :(得分:0)

$data = [
    [
        'id' => 1,
        'data' => "AAA"
    ],
    [
        'id' => 2,
        'data' => "BBB"
    ],
    [
        'id' => 3,
        'data' => "CCC"
    ]
];


$newArray = array_map(function ($value) {
    return ['test' => $value];
}, $data);

var_dump($newArray);

https://secure.php.net/manual/en/function.array-map.php