复制PHP数组,其中元素为正 [英] Copy PHP Array where elements are positive

查看:51
本文介绍了复制PHP数组,其中元素为正的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将正负组合的一个数组转移到新数组,但仅限于元素为正的情况.

trying to transfer one array with a combination of positive and negative numbers to a new array- but only where the elements are positive.

这是我到目前为止所拥有的:

This is what I have so far:

$param = array(2, 3, 4, -2, -3, -5);

function positive_function($arr) {
    foreach ($arr as &$value) {
        if($value > 0)
            return $value;
    }
}

$modParam1 = positive_function($param);

var_dump($modParam1);

我认为我的foreach语句有问题,在这里有任何明智的建议吗?

I think I have something wrong with the foreach statement, any sage advice here?

推荐答案

尝试:

$param = array(2, 3, 4, -2, -3, -5);

$positive = array_filter($param, function ($v) {
  return $v > 0;
});

print_r($positive);

http://codepad.viper-7.com/eKj7pF

这篇关于复制PHP数组,其中元素为正的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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