dox19458 2014-08-05 14:14
浏览 8
已采纳

如何在php中设置条件键=>值对?

This is what I have:

'options' => array(
    'active' => (count($panels)>=2) ? false : NULL,
    'collapsible' => true,
    'icons' => null,
     'header' => "dt"
),

I assumed that the null on the value will be enough. However, the effect we wish will only occur, if and only if, all key=>value pair don't appear on the array.

How can I make this key=>value pair to appear:

'active' => false;

If count($panels)>=2 and to not appear at all, if that's not the case?

Is there an clear, easy and understandable way to achieve this, or should I play with array merges and stuff like that?

Please advice

  • 写回答

4条回答 默认 最新

  • dongshanji3102 2014-08-05 14:17
    关注

    I'd write it as follows:

    $options = array(
        'collapsible' => true,
        'icons'       => null,
        'header'      => "dt"
    );
    
    if (count($panels) >= 2) {
        $options['active'] = false;
    }
    

    You'll obviously need to adapt this because your options array is contained within another.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构