Php 如何使用if/else更改数组元素
如何使用if/else更改数组中的值Php 如何使用if/else更改数组元素,php,arrays,if-statement,phpfox,Php,Arrays,If Statement,Phpfox,如何使用if/else更改数组中的值 if (Phpfox::isMobile()) { 'phrase' => Phpfox::getPhrase('mobiletemplate.unlike_icon'), } else { 'phrase' => Phpfox::getPhrase('like.dislike'), } 代码: 如何使用if/else插入更改代码 if (Phpfox::isMobile())
if (Phpfox::isMobile())
{
'phrase' => Phpfox::getPhrase('mobiletemplate.unlike_icon'),
}
else
{
'phrase' => Phpfox::getPhrase('like.dislike'),
}
代码:
如何使用if/else插入更改代码
if (Phpfox::isMobile())
{
'phrase' => Phpfox::getPhrase('mobiletemplate.unlike_icon'),
}
else
{
'phrase' => Phpfox::getPhrase('like.dislike'),
}
谢谢你帮助我 使用三元运算符可以解决这个逻辑问题:
'phrase' => Phpfox::isMobile() ? Phpfox::getPhrase('mobiletemplate.unlike_icon') : Phpfox::getPhrase('like.dislike'),
使用三元运算符执行内联检查:
public function getActions()
{
return array(
'dislike' => array(
'enabled' => true,
'action_type_id' => 2,
'phrase' => Phpfox::getPhrase(
Phpfox::isMobile()
? 'mobiletemplate.unlike_icon'
: 'like.dislike'
),
'phrase_in_past_tense' => 'disliked',
'item_phrase' => 'comment',
'item_type_id' => 'feed',
'table' => 'feed_comment',
'column_update' => 'total_dislike',
'column_find' => 'feed_comment_id',
'where_to_show' => array('', 'photo')
)
);
}
像这样的事
'phrase' => call_user_func(function(){
if(Phpfox::isMobile()){
return Phpfox::getPhrase('mobiletemplate.unlike_icon');
}else{
return Phpfox::getPhrase('like.dislike');
}
}), ...
更简单,但仅针对这种情况:
'phrase' => Phpfox::getPhrase(Phpfox::isMobile()? 'mobiletemplate.unlike_icon' : 'like.dislike'),
'Phpfox'=>Phpfox::getphase(Phpfox::isMobile()?'mobiletemplate.inspect_icon':'like.have')
谢谢你的回复!为什么要在数组中添加匿名函数?抱歉,伙计们,为什么这么匆忙?这不是比赛!IMHO匿名函数比三元运算符更灵活。谢谢你回答我的问题!我请求你们所有人取消自己的否决票。我的代码完美地回答了这个问题,并展示了解决这个问题的另一种方法。你的否决票是基于一个小错误代码——在一分钟内修复——现在已经过时了。谢谢你,谢谢你!谢谢你!谢谢你=谢谢你的回复!