Problem with array_merge and WHILE - PHP Codeigniter

Asked

Viewed 35 times

0

Good morning!

I have a while that reads all the lines of a specific file, inside that while it calls a method that returns a value. This value is an array, and I’m giving a merge array_for each array but when I give the Return out of while, it returns me only 1 result ;/

Follows code:

        if (!$lendo){
            echo "Erro ao abrir a URL.";
            exit;
        }

        $i = 1;
        $x = 1;
        $cod_motivo = "  ";
        $b = 4;
        $aa = "";
        $arrayRetorno = array();
        $arrayfinal = array();
        $resultado = "";
        while (!feof($lendo)) {

            $linha = fgets($lendo,401);

            $rr = "<pre>".$linha."</pre>";

            $xtamanho_linha = strlen($linha);
                    $dados = array(
                        'b' => $b,
                        'rr' => $rr,
                        'xx' => $x,
                        'i' => $i
                    );

                    //Chamada do metodo no mesmo arquivo que dá um return em um valor.

                    $teste = $this->segmento($dados,$total_itens_processados,$total_valor_nominal);

                    //teria que juntar um array vazio com o array retornando no teste;
                    $arrayfinal = array_merge($arrayRetorno,$teste);
                $i++;
        }

        //Me retorna apenas 1 valor;
        var_dump($teste); 

2 answers

0

The merge array_needs an initial attribute that will be the array in which you merge the elements. You don’t need to declare a variable, I think. Just run the function

Documentation of the merge array

$arrayFinal = array_merge($arrayFinal, [$arrayRetorno, $teste])

  • Editing... Before it was wrong

0

I think the problem is that you’re merging $arrayRetorno and $teste, but the result is being in $arrayFinal, thus the $arrayFinal will just be the $teste.

I think that would solve the problem:

$arrayfinal = array_merge($arrayfinal,$teste);

Thus, the $arrayfinal will be combined with the $teste, at each run of the loop.

Browser other questions tagged

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