PHP 循环遍历数据里中的内容

想按顺序遍历数据库中的数据,就比如1——>10 , 当10输出完后 又回到1 这样一直按顺序
思路已经有了 就比如000010000,1永远指向下一个,然后可以用where = 1 调用出来
请问怎么遍历输出啊,然后调用过的变回0,就是1用了一次就变0,然后循环完之后又从第一个0开始

下面是原来的代码,是随机算法,我现在就准备把他改成按顺序
$find_alipaygm = $this->mysql->query("client_alipaygm_automatic_account as c","c.status=4 and (c.area LIKE '".$clientCityData['city']."' or c.area LIKE '0')
and c.user_id={$user['id']}
and c.training=1
$find_alipaygm = $find_alipaygm[mt_rand(0,$count_alipaygm-1)];

讨论数量: 1

看起来你是想实现一个迭代器。


class InfiniteLoop implements Iterator {
    private $position = 0;
    private $array = array();
    private $latest_key = 0;

    public function __construct(array $array)
    {
        $this->position = 0;
        $this->array = $array;
        end($array);
        $this->latest_key = key($array);
    }

    function rewind(): void
    {
        $this->position = 0;
    }

    function current(): ?string
    {
        return $this->array[$this->position];
    }

    function key(): int
    {
        return $this->position;
    }

    function next(): void
    {
        if($this->position == $this->latest_key){
            $this->position = 0;
            return;
        }
        ++$this->position;
    }

    function valid(): bool
    {
        return isset($this->array[$this->position]);
    }

}

$array = array(1,2,3,4,5,6,7,8,9,10);

$arr = new InfiniteLoop($array);

$i = 0;
foreach($arr as $key => $value){
    $i++;

    if($i >= 100){
        break;
    }
    var_dump($key, $value);
}
4年前 评论
TomforPhP (楼主) 4年前
lxping (作者) 4年前
TomforPhP (楼主) 4年前
lxping (作者) 4年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!