Php 无键合并动态数组

Php 无键合并动态数组,php,yii2,Php,Yii2,我有两个阵列: $array1 = ['label' => 'FirstButton', 'options' => ['class' => 'btn btn-sm btn-default', 'type' => 'button']]; $array2 = ['label' => 'SecondButton', 'options' => ['class' => 'btn btn-sm btn-default', 'type' => 'button']

我有两个阵列:

$array1 = ['label' => 'FirstButton', 'options' => ['class' => 'btn btn-sm btn-default', 'type' => 'button']];
$array2 = ['label' => 'SecondButton', 'options' => ['class' => 'btn btn-sm btn-default', 'type' => 'button']];
我要做的是像这样合并这些数组:

$array3 = [$array1, array2];
$array3 = [
    ['label' => 'FirstButton', 'options' => ['class' => 'btn btn-sm btn-default', 'type' => 'button']], 
    ['label' => 'SecondButton', 'options' => ['class' => 'btn btn-sm btn-default', 'type' => 'button']]
];
$array3 = array($array1, $array2);
因此,示例结果如下所示:

$array3 = [$array1, array2];
$array3 = [
    ['label' => 'FirstButton', 'options' => ['class' => 'btn btn-sm btn-default', 'type' => 'button']], 
    ['label' => 'SecondButton', 'options' => ['class' => 'btn btn-sm btn-default', 'type' => 'button']]
];
$array3 = array($array1, $array2);
我该怎么做?我使用的是Yii2框架和bootstrap小部件ButtonGroup。按钮组小部件示例:

<?php
    echo ButtonGroup::widget([
            'buttons' => [
                ['label' => 'FirstButton', 'options' => ['class' => 'btn btn-sm btn-default', 'type' => 'button']],
                ['label' => 'SecondButton', 'options' => ['class' => 'btn btn-sm btn-default', 'type' => 'button']]
            ],
            'options' => ['class' => 'float-right']
        ]);
?>
其中,
$client[$key]
是按钮的名称。所以我的数组是动态的,我不能像这样合并数组:

$array3 = [$array1, array2];
$array3 = [
    ['label' => 'FirstButton', 'options' => ['class' => 'btn btn-sm btn-default', 'type' => 'button']], 
    ['label' => 'SecondButton', 'options' => ['class' => 'btn btn-sm btn-default', 'type' => 'button']]
];
$array3 = array($array1, $array2);
您是否尝试过:

$array3 = array( $array1, $array2 );
然后:

 echo ButtonGroup::widget([
            'buttons' => $array3,
            'options' => ['class' => 'float-right']
        ]);
更新:

[
    ['label' => 'FirstButton', 'options' => ['class' => 'btn btn-sm btn-default', 'type' => 'button']],
    ['label' => 'SecondButton', 'options' => ['class' => 'btn btn-sm btn-default', 'type' => 'button']]
]
只是另一种定义样式(自PHP5.4起):

从PHP5.4开始,您还可以使用短数组语法,它将数组()替换为[]

来源

因此,您应该能够直接使用:

<?php
    echo ButtonGroup::widget([
            'buttons' => $buttonGroups,
            'options' => ['class' => 'float-right']
        ]);
?>

您可以使用
array\u merge()
方法在一行中完成

例如:

echo ButtonGroup::widget([
        'buttons' => array_merge($array1, array2),
        'options' => ['class' => 'float-right']
    ]);    

我有动态数组,所以我不能只使用$array3=array($array1,$array2);谢谢塔哈·帕克苏的回答,你是对的。我从您提供的源代码中阅读了有关数组的内容,并仔细检查了我的代码。然后我复制了您给定的代码,它工作了,所以我的错误是使用了
'buttons'=>[$buttonGroups],
而不是
'buttons'=>$buttonGroups,
。也许我遗漏了一些东西,但控制器中的foreach循环之后,
$buttonGroups
是否与您要查找的结构完全相同?它的结构如下:[0]=>array1;[1] =>阵列2;但是ButtonGroup需要[array1,array2],而不需要按键。buttonGroup接受的格式如上面的buttonGroup示例所示。
$x=[$array1,$array2]
生成的数组具有键
0
1
$x=array($array1,$array2)
$x=array(0=>$array1,1=>$array2)
$x=array2()$x[]=阵列1美元$x[]=阵列2美元-它们无法区分。[a1,a2]只是语法上的糖分,因此开发人员必须键入更少的内容,但它会产生相同的、完全相同的内部表示。那么,我如何从这个语法
$x=[[0]=>$a1[1]=>$a2]
转换成这个语法
$x=[$a1,$a2]
?因为ButtonGroup只接受这个语法,
$x=[$a1,$a2]
你什么都不做……因为它已经存在了。。。。函数/方法接受具有特定结构的数组;您已经拥有了该结构-无论在源代码中该结构是通过
数组(0=>x,1=>y)
还是
[x,y]
创建的;没关系;在运行时,两者无法区分/相同。