使用三元表达式返回会在第一次迭代时中断 foreach 循环

发布于 2024-10-17 22:12:29 字数 1624 浏览 7 评论 0原文

好的,我测试了以下内容,我会让您知道我发现了什么:

echo ('-1' < 0) ? 'true' : 'false'; // will echo "true"
echo ('1' > 0) ? 'true' : 'false'; // will echo "true"
# Notice that '-1' and '1' are strings

现在让我们获取一个数组,该数组在过滤所有结果后来自数据库,以便仅获取具有 UID = 1 的行。

$this->a = array(
    [0] => array(
        'UID' => '1',
        'PID' => '91',
        'Amount' => '-1'
    ),
    [1] => array(
        'UID' => '1',
        'PID' => '92',
        'Amount' => '1'
    ),
    [2] => array(
        'UID' => '1',
        'PID' => '93',
        'Amount' => '1'
    )
);

现在我想创建一个函数 posAmount($PID) ,如果 'Amount' > 则返回 true如果 'Amount' 0false 0 。 (注意:金额 = 0 是我并不关心的)。另外,我想编写一个名为 negAmount($PID) 的类似函数,它返回与第一个完全相反的函数。现在,我想向您介绍我的孪生函数:

public function posAmount($pid)
{
    foreach ($this->a as $a)
    {
        if (count($this->a) == 0) { return false; }
        return ($a['PID'] == $pid and $a['Amount'] > 0) ? true : false;
    }
}

public function negAmount($pid)
{
    foreach ($this->a as $a)
    {

        if (count($this->a) == 0) { return false; }
        return ($a['PID'] == $pid and $a['Amount'] < 0) ? true : false;
    }
}

很酷的事实是,关于第一个数组(我使用 var_dump() 检查过,在整个脚本中保持了其本质):

$istance->negAmount(91); // Returns true, as expected
$istance->posAmount(92); // Returns false, as NOT expected.
# Why do God wants me to get mad?

Ok, i tested what follows and i'll just let you know what i discovered:

echo ('-1' < 0) ? 'true' : 'false'; // will echo "true"
echo ('1' > 0) ? 'true' : 'false'; // will echo "true"
# Notice that '-1' and '1' are strings

Now let's take an array, coming from the database after filtering all the result in order to get only rows with UID = 1.

$this->a = array(
    [0] => array(
        'UID' => '1',
        'PID' => '91',
        'Amount' => '-1'
    ),
    [1] => array(
        'UID' => '1',
        'PID' => '92',
        'Amount' => '1'
    ),
    [2] => array(
        'UID' => '1',
        'PID' => '93',
        'Amount' => '1'
    )
);

Now i want to create a function posAmount($PID) that returns true if 'Amount' > 0 or false if 'Amount' < 0. (Notice: Amount = 0 is something i don't really care). Also i'd like to write as similar function called negAmount($PID) that returns the exactely opposite of the first. I'd like, now, to introduce you to my twin functions:

public function posAmount($pid)
{
    foreach ($this->a as $a)
    {
        if (count($this->a) == 0) { return false; }
        return ($a['PID'] == $pid and $a['Amount'] > 0) ? true : false;
    }
}

public function negAmount($pid)
{
    foreach ($this->a as $a)
    {

        if (count($this->a) == 0) { return false; }
        return ($a['PID'] == $pid and $a['Amount'] < 0) ? true : false;
    }
}

The cool fact is that, regarding the first array (which, i checked with var_dump() keeps its nature trough the entire script):

$istance->negAmount(91); // Returns true, as expected
$istance->posAmount(92); // Returns false, as NOT expected.
# Why do God wants me to get mad?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

枯叶蝶 2024-10-24 22:12:29

问题是您总是在 foreach 循环的第一次迭代中返回。您应该像这样重写函数:

public function negAmount($pid) {
    if (count($this->a) == 0) { return false; }
    foreach ($this->a as $a) {
        if ($a['PID'] == $pid) {
            if ($a['Amount'] < 0) {
                return true;
            }
        }
    }
    return false;
}

public function posAmount($pid) {
    if (count($this->a) == 0) { return false; }
    foreach ($this->a as $a) {
        if ($a['PID'] == $pid) {
            if ($a['Amount'] > 0) {
                return true;
            }
        }
    }
    return false;
}

The problem is that you are always returning on the first iteration of the foreach loop. You should rewrite the functions like this:

public function negAmount($pid) {
    if (count($this->a) == 0) { return false; }
    foreach ($this->a as $a) {
        if ($a['PID'] == $pid) {
            if ($a['Amount'] < 0) {
                return true;
            }
        }
    }
    return false;
}

public function posAmount($pid) {
    if (count($this->a) == 0) { return false; }
    foreach ($this->a as $a) {
        if ($a['PID'] == $pid) {
            if ($a['Amount'] > 0) {
                return true;
            }
        }
    }
    return false;
}
月寒剑心 2024-10-24 22:12:29

可能只是演示代码中的拼写错误,但 posAmount 方法正在循环 $this->a,而另一个方法正在循环 $this- >votes - OP更正

你的代码中有一些奇怪的东西。为什么要在 foreach 循环中检查 $this->a 的计数?在开始循环之前检查计数会更有意义。

另外,您的比较中存在一些逻辑错误。您只是比较循环中的第一次迭代...它将为数组的第一个索引返回 true 或 false,甚至从不查看其他索引。在比较并返回任何内容之前,您需要匹配循环中的 PID。就像这样:

public function posAmount($pid)
{
    if (count($this->a) == 0) { return false; }
    foreach ($this->votes as $a) {
        if ($a['PID'] == $pid)
        return $a['Amount'] > 0 ? true : false;
    }
    return false;
}

public function posAmount($pid)
{
    if (count($this->a) == 0) { return false; }
    foreach ($this->votes as $a) {
        if ($a['PID'] == $pid)
        return $a['Amount'] < 0 ? true : false;
    }
    return false;
}

May just be a typo in your demo code, but posAmount method is looping $this->a, whereas the other is looping $this->votes - OP corrected

You've got some odd things in your code. Why are you checking the count of $this->a from within a foreach loop? It would make more sense to check the count before you start looping.

Also, you've got some logic errors in your comparison. You're only comparing the first iteration through the loop... it will either return true or false for the first index of the array and never even look at the others. You'll want to match the PID in the loop before you compare - and return - any thing. Like so:

public function posAmount($pid)
{
    if (count($this->a) == 0) { return false; }
    foreach ($this->votes as $a) {
        if ($a['PID'] == $pid)
        return $a['Amount'] > 0 ? true : false;
    }
    return false;
}

public function posAmount($pid)
{
    if (count($this->a) == 0) { return false; }
    foreach ($this->votes as $a) {
        if ($a['PID'] == $pid)
        return $a['Amount'] < 0 ? true : false;
    }
    return false;
}
一抹淡然 2024-10-24 22:12:29

问题是您试图将字符串与 int 进行比较,而不尝试对其进行转换。将 $a['Amount'] 更改为 (int)$a['Amount'] 并看看会发生什么。

The problem is you're trying to compare a string to an int without trying to convert it. Change $a['Amount'] to (int)$a['Amount'] and see what happens.

原来是傀儡 2024-10-24 22:12:29

所以在这里你迭代 $this->a:

public function posAmount($pid)
{
    foreach ($this->a as $a)

但这里 $this->votes:

public function posAmount($pid)
{
    foreach ($this->a as $a)

打字错误或者什么......

So here you iterate $this->a:

public function posAmount($pid)
{
    foreach ($this->a as $a)

But here $this->votes:

public function posAmount($pid)
{
    foreach ($this->a as $a)

Typo or what...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文