从函数php总计返回值

时间:2016-07-15 10:48:58

标签: php function laravel

我正在使用它来从CSV中提取一些facebook数据

https://github.com/Maatwebsite/Laravel-Excel

public function lifetime_likes_by_gender_and_age_f18_24()
{
    $result = $this->reader->select(array('lifetime_likes_by_gender_and_age_f18_24'))->get();
    return (int)$result->last()->sum();

}

public function lifetime_likes_by_gender_and_age_f25_34()
{
    $result = $this->reader->select(array('lifetime_likes_by_gender_and_age_f25_34'))->get();
    return (int)$result->last()->sum();;
}

public function lifetime_likes_by_female()
{
    return $this->lifetime_likes_by_gender_and_age_f18_24() + $this->lifetime_likes_by_gender_and_age_f25_34();
}

问题是第一个值返回12,第二个值是112.所以答案应该是124,但它返回136,这表明它加倍了第一个值。

我为什么以及如何解决这个问题的原因? PHP版本是7。

1 个答案:

答案 0 :(得分:0)

我猜测select方法会向读者介绍一个关于所选列的状态。 因此,当你运行18_24方法时,你只需选择它,但是当你运行25_34时,之前的列从未被取消选择,所以你再次选择它们。

请你试试

public function lifetime_likes_by_gender_and_age_f18_24()
{
    $result = $this->reader->get(array('lifetime_likes_by_gender_and_age_f18_24'));

    return (int)$result->last()->sum();
}

public function lifetime_likes_by_gender_and_age_f25_34()
{
    $result = $this->reader->get(array('lifetime_likes_by_gender_and_age_f25_34'));

    return (int)$result->last()->sum();
}

我会阅读更多的库源代码,但应该是它。