php 聪明的 in_array 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14400460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Smarty in_array value
提问by MyMomSaysIamSpecial
I have array like this
我有这样的数组
{
"sCode":"05",
"sCodeName":"critical_tight_connection",
"iSeverity":1,
"aData":{
"iLevelOfDetailt":2,
"iDuration":35,
"sLabel":"Labai trumpas pers\u0117dimas, 35 min.",
"sLink":""
}
}
i am printing him with smarty (array is not serialized, i ve did it for your сonveniece)
我正在用 smarty 打印他(数组未序列化,我已经为您方便了)
{if !empty( $aSegment.aNotices.aStop )}
<ul>
{foreach from=$aSegment.aNotices.aStop item=aNotice}
<li>
<img class="{$aNotice.sCodeName}" />
{$aNotice.sLabel}
</li>
{/foreach}
</ul>
{/if}
how to check with smarty if '05' is exist in aNotices.aStop.sCode ? (before foreach cycle)
如何使用 smarty 检查 aNotices.aStop.sCode 中是否存在“05”?(在 foreach 循环之前)
Tried this
试过这个
{if in_array('05', $aSegment.aNotices.aStop)}
exist
{/if}
回答by Jeff Bic
you can use this :
你可以使用这个:
{if '05'|in_array:$aSegment.aNotices.aStop}EXIST{/if}
回答by hek2mgl
Do you mean something like this?
你的意思是这样吗?
{if $aNotice.sCode == '05'} ....
回答by MyMomSaysIamSpecial
Thank you hek2mgland Jeff Bic, modified your answers and this works fine :
谢谢hek2mgl和Jeff Bic,修改了你的答案,这很好用:
{if !$aSegment.aNotices.aStop|@count gt 0}
{t lang='en'}Stop duration{/t}{if $server.lang != 'en'} / {t}Stop duration{/t}{/if}: {$aSector.aStops.$iSegmentIndex|mins_to_time}
{/if}

