PHP的foreach循环条件?

时间:2020-05-10 14:47:34

标签: php

我只想在点大于水平时循环。 我想要的结果是“ reward0”

积分之和为80,大于50但小于100,因此只需要一个奖励。

我这样尝试,但没有运气。

    private $connection;

    public function __construct()
    {
        $this->connection = Http::withBasicAuth($this->username, $this->password);
    }

    public function apicall($id) {
        $response = $this->connection->get('api/url/' . $id);
        // ...
    }

1 个答案:

答案 0 :(得分:2)

如果我理解您的逻辑,那么这就是您想要的,

<?php
$levels = [50, 100, 150, 200];
$points = 0;
$rewards = [];
$points = count($levels) * 20;
$rewards = array_filter($levels, function($n) use ($points){ 
    return $n < $points;
});
print_r($rewards);

演示: https://3v4l.org/3VJTR