限制foreach输出4个结果 [英] Limit foreach to output 4 results

查看:88
本文介绍了限制foreach输出4个结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请有人帮助将以下foreach代码限制为仅按desc顺序输出4个结果.非常感谢你!

someone please help to limit the below foreach code to output 4 results only and in desc order. Thank you very much!

foreach( $users_who_like as $id ) :

if ( $id != $user_id )
$output .= ' &middot <a href="' . bp_core_get_user_domain( $id ) . '" title="' . bp_core_get_user_displayname( $id ) . '">' . bp_core_get_user_displayname( $id ) . '</a>';

endforeach;

推荐答案

您可以使用count并检查它是否为4 像这样

You may use count and check if it's 4 like this

$count = 4; 

foreach( $users_who_like as $id ){

if ( $id != $user_id )
$output .= ' &middot <a href="' . bp_core_get_user_domain( $id ) . '" title="'.bp_core_get_user_displayname( $id ) . '">' . bp_core_get_user_displayname( $id ) .'</a>';

 if(count <=0)
 break; //will break if statement and foreach

 $count--; // reduce it by one

}

这篇关于限制foreach输出4个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆