在PHP

时间:2017-11-03 10:36:30

标签: php merge

我有两个不同尺寸的不同数组,我需要合并才能获得具有特定结构的结果:

第一个:

Array
(
    [0] => Array
        (
            [0] => 2017-11-03
            [1] => 2017-11-05
            [2] => 1
        )

    [1] => Array
        (
            [0] => 2017-11-23
            [1] => 2017-11-25
            [2] => 1
        )

)

第二个:

Array
(
    [0] => 2017-12-26
    [1] => 2018-01-30
)

结果应为:

Array
    (
        [0] => Array
            (
                [0] => 2017-11-03
                [1] => 2017-11-05
                [2] => 1
            )

        [1] => Array
            (
                [0] => 2017-11-23
                [1] => 2017-11-25
                [2] => 1
            )

        [2] => Array
            (
                [0] =>2017-12-26
                [1] => 2018-01-30
                [2] => 1
            )

    )

我尝试使用array_merge但它不起作用,因为它们的维度不同。我还需要第二个标签中的一个元素([2] => 1)。

3 个答案:

答案 0 :(得分:0)

你描述的是追加,而不是合并。试试这个:

$arraySecond[] = 1;           // This adds [2]=> 1
$arrayFirst[] = $arraySecond; // This adds second array to end of first

答案 1 :(得分:0)

您的例子:

@Component({
    selector: 'app-parent',
    templateUrl: './app-parent.component.html',
    styleUrls: ['./app-parent.component.scss']
})
export class ParentComponent {
}

@Component({
    selector: 'app-child1',
    templateUrl: '../parent/app-parent.component.html',
    styleUrls: ['../parent/app-parent.component.scss'],
    encapsulation: ViewEncapsulation.None
})
export class Child1Component extends ParentComponent {
}

@Component({
    selector: 'app-child2',
    templateUrl: '../parent/app-parent.component.html',
    styleUrls: ['../parent/app-parent.component.scss'],
    encapsulation: ViewEncapsulation.None
})
export class Child2Component extends ParentComponent {
}

答案 2 :(得分:0)

<?php

$array=Array
(
    0 => Array
    (
        0 => '2017-11-03',
            1 => '2017-11-05',
            2 => '1',
        ),

    1 => Array
(
    0=> '2017-11-23',
            1 => '2017-11-25',
            2 => '1'
        ),


);

$arraySmall=Array
(
    0 => '2017-12-26',
    1 => '2018-01-30'
);


array_push($arraySmall, "1");
array_push($array, $arraySmall);
echo'<pre>';
print_r($array);

输出是:

Array
(
    [0] => Array
        (
            [0] => 2017-11-03
            [1] => 2017-11-05
            [2] => 1
        )

    [1] => Array
        (
            [0] => 2017-11-23
            [1] => 2017-11-25
            [2] => 1
        )

    [2] => Array
        (
            [0] => 2017-12-26
            [1] => 2018-01-30
            [2] => 1
        )

)

这种方式即使没有这一行array_push($arraySmall, "1");也可以工作 你可以尝试一下。为了“合并”你需要相同的尺寸,但对于“推”,你不需要。所以,如果你推荐我告诉你的那条线,输出将如下所示:

Array
(
    [0] => Array
        (
            [0] => 2017-11-03
            [1] => 2017-11-05
            [2] => 1
        )

    [1] => Array
        (
            [0] => 2017-11-23
            [1] => 2017-11-25
            [2] => 1
        )

    [2] => Array
        (
            [0] => 2017-12-26
            [1] => 2018-01-30
        )

)