Error when separating arrays in PHP

0

I have a PHP class responsible for sending invoices, however it returns the following error, as if trying to pass an array to the variable:

[13-Mar-2018 14:08:26 America/Sao_Paulo] PHP Notice:  Array to string conversion in C:\...ToolsNFePHP.class.php on line 5212

By the code, the variable $msg should already send a string , already tried to convert from Array to String , but the error continues .

Follow the code:

/**
 * pSetError
 * Adiciona descrição do erro ao contenedor dos erros
 *
 * @name pSetError
 * @param   string $msg Descrição do erro
 * @return  none
 */
private function pSetError($msg)
{
    $this->errMsg .= "$msg\n";
    $this->errStatus = true;
}

This happens only in this case, other objects send work normally. I think I might be adding more than one error message to the same variable and causing the error, but without seeing it I can not fix it.

    
asked by anonymous 13.03.2018 / 17:53

1 answer

1

The error that is giving (Array to string conversion), you are trying to separate a String in an Array, which are different things.

    private function pSetError($msg = array())
{
    $MsgErro = explode(',', $msg);
    $this->errMsg .= "$MsgErro\n";
    $this->errStatus = true;
}
  

implode - joins elements of an array into a string    Implode

     

explode - Splits a string into strings    Explode

Explode and Implode is for "Strings" and not Array if you wanted to break the Array into "chunks", you can use array_chunk . With it you can break into several pieces as you wish;

$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(array_chunk($input_array, 2));
print_r(array_chunk($input_array, 2, true));

Output the above code:

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [0] => c
            [1] => d
        )

    [2] => Array
        (
            [0] => e
        )

)
Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
        )

    [1] => Array
        (
            [2] => c
            [3] => d
        )

    [2] => Array
        (
            [4] => e
        )

)

You can also use the array_slice

$input = array("a", "b", "c", "d", "e");

$output = array_slice($input, 2);      // returns "c", "d", and "e"
$output = array_slice($input, -2, 1);  // returns "d"
$output = array_slice($input, 0, 3);   // returns "a", "b", and "c"


print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));

The code above will look like this:

Array
(
    [0] => c
    [1] => d
)
Array
(
    [2] => c
    [3] => d
}
    
13.03.2018 / 18:08