亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有更好的方法來使用樹枝測試嵌套數組中存在的值

有沒有更好的方法來使用樹枝測試嵌套數組中存在的值

PHP
森欄 2023-05-12 15:54:54
我有一個模態,它有使用這個數據數組的樹枝渲染的按鈕"buttons" => [    [        "title" => "Copy",        "type" => "button",        "attributes" => [            "data-action" => "confirm"        ],        "class" => "btn-primary",    ],    [        "title" => "Cancel",        "type" => "button",        "attributes" => [            "aria-label" => "Close"        ],        "class" => "btn-light",    ]]如果已經有一個屬性為“aria-labal='Close'”的按鈕,我希望模式不在頂角顯示 [x],因此我添加了這組嵌套的 if 語句和 for 循環。{% set hideBtnClear = false %}{% for btn in modal.buttons %}    {% if btn.attributes %}        {% for key, value in btn.attributes %}            {% if key == "aria-label" and value == "Close" %}                {% set hideBtnClear = true %}            {% endif %}        {% endfor %}    {% endif %}{% endfor %}{% if hideBtnClear == false %}    [x] <--{% endif %}它有效但不是很優雅。有什么辦法可以改善它嗎?
查看完整描述

2 回答

?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

您也可以使用過濾器filter來解決這個問題

{%?if?btns|filter(v?=>?v.attributes['aria-label']|default?==?'Close')?|?length?==?0?%}
????[?X?]?
{%?endif?%}

使用not代替== 0也有效

{% if not btns|filter(v => v.attributes['aria-label']|default == 'Close') | length %}


查看完整回答
反對 回復 2023-05-12
?
白豬掌柜的

TA貢獻1893條經驗 獲得超10個贊

變化不大,但如果您知道 中所需的鍵btn.attributes,則只需檢查此鍵是否存在及其值:


{% set hideBtnClear = false %}

{% for btn in modal.buttons %}

    {% if btn.attributes['aria-label'] is defined and btn.attributes['aria-label'] == "Close" %}

        {% set hideBtnClear = true %}

    {% endif %}

{% endfor %}

{% if hideBtnClear == false %}

    [x] <--

{% endif %}


查看完整回答
反對 回復 2023-05-12
  • 2 回答
  • 0 關注
  • 176 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號