Convert an array of numbers into a single string in php

0

How do I transform an array of numbers into a single string in php?

In the code below the variable $ result is the array of numbers, and I tried to convert it to a single string with the implode () function. But it does not seem to work because php gives the message 'Notice: Array to string conversion in ...' (QUERY LINE

And in the database did not work persistence ...

How to do this conversion?

//Result é o array de números. Preciso convertê-lo para uma String...
$resultado = implode($result);

        mysqli_query($con,"INSERT INTO forum (codUsuario,titulo,mensagem) VALUES('$result','$titulo','$mensagem');");
    
asked by anonymous 27.05.2018 / 22:47

3 answers

1

In the implode function, I put the empty delimiter and it worked:

$resultado = implode('', $result);
    
27.05.2018 / 23:00
1

The problem is that you are including $result in the query and not $resultado , you are doing:

$resultado = implode($result);

mysqli_query($con,"INSERT forum (...) VALUES('$result','$titulo','$mensagem');");
                                                 ^^^^

Just change to $resultado and it will work normally. The implode , as documented, accepts to invert the parameters. That is both implode($array, '') and implode('', $array) work, as well as only use implode($array) in this case.

    
28.05.2018 / 00:44
0

Another option - no delimiter needed

Ideone

$meuArray = array( 1, 2, 3 );
$result="";
foreach( $meuArray  as  $valor ) {
    $result .= $valor;
}
echo $result; // 123

Sandbox

$meuArray =  [1, 2, 3];
$result="";
foreach( $meuArray  as  $valor ) {
    $result .= $valor;
}
echo $result; 
    
27.05.2018 / 23:27