Error separating arrays in PHP

Asked

Viewed 211 times

0

I have a PHP class responsible for sending tax notes, however it returns the following error, as if trying to pass an array to 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 code, the variable $msg should already send a string, I have tried to convert from Array for String, but the error continues.

Follows 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 sending objects work normally. I believe I may be aggregating more than one error message in the same variable and causing the error, but without seeing it I cannot correct.

  • Actually you are trying to concatenate the array values and not exploding/separating. You are probably passing a String for the method pSetError, while the correct one would be to pass one Array.

  • 1
  • The code seems correct what may be wrong is argument in the method call.

  • Use a string instead of a array. You can use the implode(",", $array); before accessing the function.

1 answer

1


The error that is giving ( Array to string Conversion ), you’re trying to separate a String into 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 Explodes

Explode and Implode serves for "Strings" and not Array, in case you wanted to break the Array into "pieces", you can use the array_chunk. With it you can break into several pieces 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));

Exit from 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 come out like this:

Array
(
    [0] => c
    [1] => d
)
Array
(
    [2] => c
    [3] => d
}
  • Thanks, reevaluating the code really it should be sending a string already, not an array.

  • I ended up editing the question... I believe it is now clearer.

Browser other questions tagged

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