php 在类中循环遍历数组

5ssjco0h  于 2023-06-04  发布在  PHP
关注(0)|答案(1)|浏览(490)

我从我的一个类中得到了一些非常奇怪的输出,在这里:

class group
{
    public $myArray = array(); // an array of fraction objects

    // find the smallest fraction
    private function smallest()
    {
        $result = array('num'=>NULL, 'den'=>NULL); // this is what is returned
        // initialize temp variables
        $fractionObj = reset($this->myArray); // pointer to first element of array
        while($fractionObj->dropped == 1)
        {
            $fractionObj = next($this->myArray); // move the pointer ahead until finding a fraction that is not already dropped. RESULTS IN 'Notice: Trying to get property of non-object' ERROR on this line
        }
        $decimal = ($fractionObj->numerator/$fractionObj->denominator); // initialized to the first fraction value that is not dropped
        $result['num'] = $fractionObj->numerator;
        $result['den'] = $fractionObj->denominator;
        $lowest = $fractionObj;
        foreach($myArray as $a)
        {
            if($a->dropped == 0 && $a->numerator/$a->denominator < $decimal)
            {
                $decimal = $a->numerator/$a->denominator;
                $result['num'] = $a->numerator;
                $result['den'] = $a->denominator;
                $lowest->dropped = true; // mark this value as dropped so it is ignored for the next call to smallest()
            }
        }
        return $result;
    }
}

class fraction
{
    public $numerator;
    public $denominator;
    public $dropped = false;
}

我在这里有一个组类,它包含一个分数类对象数组。当一个新的组对象被创建时,它是用放置在数组中的分数构造的,并且所有工作正常。
我试图找到最小分数的x个数(基于它们的十进制值),并将最小的分数标记为“dropped”,这意味着它们仍然会留在数组中,但我们会忽略它们。这就是$dropped变量在fraction类中所表示的。我用一个调用smallest()函数的循环来实现这一点
问题是,我得到的通知,我试图获得一个非对象的属性,即$fractionObj->下降。但是,如果我var_dump($fractionObj),它告诉我它是一个对象。有没有一种方法可以将数组指针前进到数组中的下一个对象,而不使用next(),因为我认为这是问题所在。

laximzn5

laximzn51#

我可以用下面的解决方案来解决。我相信还有更好的办法。如果能够使用next()进行迭代会非常方便,但我想这并不能直观地处理对象。

class group
{
    public $myArray = array(); // an array of fraction objects

    // find the smallest fraction
    private function smallest()
        {
        $result = array('num'=>NULL, 'den'=>NULL); // this is what is returned
        foreach($this->myArray as $a)
        {
            if($a->dropped == false)
            {
                if(!isset($val))
                {
                    $val = $a->percent();
                    $lowest = $a;
                    $result['num'] = $a->nominator;
                    $result['den'] = $a->denominator;
                }else{
                    if($a->percent() < $val)
                    {
                        $val = $a->percent();
                        $lowest = $a;
                        $result['num'] = $a->nominator;
                        $result['den'] = $a->denominator;
                    }
                }
            }
        }
        $lowest->drop();
        return $result;
    }
}

class fraction
{
    public $numerator;
    public $denominator;
    public $dropped = false;

    public function percent()
    {
        return $this->numerator/$this->denominator;
    }

    public function drop()
    {
         $this->dropped = true;
    }
}

相关问题