php如何用循环遍历求数组的最大值,php如何遍历数组直到满足条件?

您只需调用array_filter()即可仅获取数组中的非空元素。 array_filter()可以使用回调函数来确定要删除的内容,但在这种情况下,empty()将评估为FALSE并且不需要回调。将仅删除评估empty() == TRUE的任何值。

$p=array($img1, $img2.....$img20);

$nonempty = array_filter($p);

// $nonempty contains only the non-empty elements.

// Now dow something with the non-empty array:

foreach ($nonempty as $value) {

something();

}

// Or use the first 10 values of $nonempty

// I don't like this solution much....

$i = 0;

foreach ($nonempty as $key=>$value) {

// do something with $nonempty[$key];

$i++;

if ($i >= 10) break;

}

// OR, it could be done with array_values() to make sequential array keys:

// This is a little nicer...

$nonempty = array_values($nonempty);

for ($i = 0; $i<10; $i++) {

// Bail out if we already read to the end...

if (!isset($nonempty[$i]) break;

// do something with $nonempty[$i]

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值