删除第一级多维数组PHP

时间:2017-04-20 15:05:49

标签: php arrays multidimensional-array

我有一个数组,我不想删除标识符[0]的第一级。我认为有一个函数可以重写以下代码:

Array
(
    [0] => Array
        (
            [id] => 20
            [parent_id] => 15
            [status] => 1
        )

)
Array
(
    [0] => Array
        (
            [id] => 15
            [parent_id] => 1
            [status] => 1
        )

)

我想要这样

Array
(
    [0] => Array
        (
            [id] => 20
            [parent_id] => 15
            [status] => 1
        )
    [1] => Array
        (
            [id] => 15
            [parent_id] => 1
            [status] => 1
        )
)

我尝试使用以下代码,但我希望它们位于同一个数组中。这有功能吗?

        $processed = array();
        foreach($category[0] as $key => $value) {
            $processed[$key] = $value;
        }
Array
(
    [id] => 20
    [parent_id] => 15
    [status] => 1
)
Array
(
    [id] => 15
    [parent_id] => 1
    [status] => 1
)
Array
(
    [id] => 1
    [parent_id] => 0
    [status] => 1
)

我弄清楚但是有这个功能吗?

$output = array();

while(true) {
// code ..
$processed = array();
foreach($category[0] as $key => $value) {
    $processed[$key] = $value;
}
$output[] = $processed;

//code..
}

print_r($output)

1 个答案:

答案 0 :(得分:0)

听起来你想要array_merge:http://php.net/manual/en/function.array-merge.php

在您的示例代码中,您可以这样使用它:

$firstArray = Array
(
[0] => Array
    (
        [id] => 20
        [parent_id] => 15
        [status] => 1
    )
);

$secondArray = Array
(
[0] => Array
    (
        [id] => 15
        [parent_id] => 1
        [status] => 1
    )
)

$thirdArray = Array
(
[0] => Array
    (
        [id] => 15
        [parent_id] => 1
        [status] => 1
    )
)

$processed = array_merge($firstArray, $secondArray, $thirdArray);

如果上述方法不切实际,您可以执行以下操作:

$processed = array();
foreach($category[0] as $key => $value) {
    $processed = array_merge($processed, $value);
}