0

我有一个这样的数组

'sections'        => array(
  array(
    'title'       => 'License',
    'id'          => 'license'
    ),
  array(
    'title'       => 'General',
    'id'          => 'general'
    ),
  array(
    'title'       => 'Header',
    'id'          => 'header'
    ),
  array(
    'title'       => 'Footer',
    'id'          => 'footer'
    ),
  array(
    'title'       => 'Slider',
    'id'          => 'slider'
    ),
)

如果许可证无效,我只想显示许可证选项卡。所以我想在这样的数组中使用 if else 语句

    'sections'        => array(
      array(
        'title'       => 'License',
        'id'          => 'license'
        ),

if ($license_valid) {
      array(
        'title'       => 'General',
        'id'          => 'general'
        ),
      array(
        'title'       => 'Header',
        'id'          => 'header'
        ),
      array(
        'title'       => 'Footer',
        'id'          => 'footer'
        ),
      array(
        'title'       => 'Slider',
        'id'          => 'slider'
        ),

  }
)

有人能告诉我正确的使用方法吗?

4

2 回答 2

1
$array = array(...);

if ($license_valid) {
   $additional = array(
    array(
        'title'       => 'General',
        'id'          => 'general'
        ),
      array(
        'title'       => 'Header',
        'id'          => 'header'
        ),
      array(
        'title'       => 'Footer',
        'id'          => 'footer'
        ),
      array(
        'title'       => 'Slider',
        'id'          => 'slider'
        ),
   );

   $array = array_merge($array, $additional)
}
于 2013-10-11T11:40:13.983 回答
0

好吧,array_merge 很好,但这里有一种不同的方法:

$arr = array('sections' => array(
    array(
        'title'       => 'License',
        'id'          => 'license'
    )
);

if ($license_valid) {
    $arr['sections'][] = array(
        'title'       => 'General',
        'id'          => 'general'
    );
    $arr['sections'][] = array(
        'title'       => 'Header',
        'id'          => 'header'
    );
    $arr['sections'][] = array(
        'title'       => 'Footer',
        'id'          => 'footer'
    );
    $arr['sections'][] = array(
        'title'       => 'Slider',
        'id'          => 'slider'
    );
}
于 2013-10-11T11:45:13.147 回答