duanba4942 2015-01-21 08:46
浏览 16
已采纳

合并没有键的动态数组

I have two arrays:

$array1 = ['label' => 'FirstButton', 'options' => ['class' => 'btn btn-sm btn-default', 'type' => 'button']];
$array2 = ['label' => 'SecondButton', 'options' => ['class' => 'btn btn-sm btn-default', 'type' => 'button']];

What I want to do, is to merge these arrays like this:

$array3 = [$array1, array2];

So example result whould be like this:

$array3 = [
    ['label' => 'FirstButton', 'options' => ['class' => 'btn btn-sm btn-default', 'type' => 'button']], 
    ['label' => 'SecondButton', 'options' => ['class' => 'btn btn-sm btn-default', 'type' => 'button']]
];

How can I do that? I'm using Yii2 framework, and bootstrap widget ButtonGroup. ButtonGroup widget example:

<?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']
        ]);
?>

The reason I need to merge these arrays like this is because my ButtonGroup is dynamic, and in view file I want to use variable from controller $buttonGroup:

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

Update In controller I have this:

$buttonGroups = [];
foreach($client as $key => $value) {
    $buttonGroups[] = ['label' => $client[$key], 'options' => ['class' => 'btn btn-sm btn-default', 'type' => 'button']];
}

where $client[$key] is the name of the button. So my arrays are dynamic and I can't just merge arrays like this:

$array3 = array($array1, $array2);
  • 写回答

2条回答 默认 最新

  • dongzi5062 2015-01-21 08:49
    关注

    did you try:

    $array3 = array( $array1, $array2 );
    

    and then :

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

    Update:

    [
        ['label' => 'FirstButton', 'options' => ['class' => 'btn btn-sm btn-default', 'type' => 'button']],
        ['label' => 'SecondButton', 'options' => ['class' => 'btn btn-sm btn-default', 'type' => 'button']]
    ]
    

    is simply another definition style of (since PHP5.4):

    As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].

    Source: http://php.net/manual/en/language.types.array.php

    array(
        array('label' => 'FirstButton', 'options' => ['class' => 'btn btn-sm btn-default', 'type' => 'button']),
        array('label' => 'SecondButton', 'options' => ['class' => 'btn btn-sm btn-default', 'type' => 'button'])
    )
    

    so you should be able to use directly:

    <?php
        echo ButtonGroup::widget([
                'buttons' => $buttonGroups,
                'options' => ['class' => 'float-right']
            ]);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现