PHP error: "Can’t use Function Return value in write context"

Asked

Viewed 342 times

3

This error is coming to me and I’m not getting why. The line of code is as follows:: $matrix(0, $key)=$quantidade['id_product'];

The complete function is as follows:

public function sumQuantidadesPorProduto() {

        $quantidades = $this->getAllQuantidadesLocais();

        $sum = 0;
        $matrix = array();
        foreach ($quantidades as $key => $quantidade) {
            $sum+=intval($quantidade['quantidade']);
            $matrix(0, $key) = $quantidade['id_product'];
            $matrix(1, $key) = $sum;
        }

        return $matrix;
}

1 answer

4

You’re making a syntax error.

To access arrays you must use [ and not (.

Change your code to look like this:

public function sumQuantidadesPorProduto() {

    $quantidades = $this->getAllQuantidadesLocais();

    $sum = 0;
    $matrix = array();
    foreach ($quantidades as $key => $quantidade) {
        $sum+=intval($quantidade['quantidade']);
        $matrix[0][$key] = $quantidade['id_product'];
        $matrix[1][$key] = $sum;
    }

    return $matrix;
}
  • I can’t believe I didn’t even notice it, and the problem is I had it earlier ahah Many hours already programming... thank you, hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.