dgtal dgtal - 25 days ago 8
MySQL Question

Php array to longtext with new lines in database

I have an array of values that i would like to save in a longtext database field, one per line.

I'm using Yii 2.

If i save it like this:

$object->field = implode('\n', $array);


Then i get it in the database as a string with \n, and i can't get it separated by a new line with

nl2br($value);


But if i save it manually inside the database with phpMyAdmin in multiple lines, it works. How can i get a text string that holds the new lines when saving to the db?

Answer Source

In php if you want to add new line then you have to use \n with double-quotes. So code need to be:-

$object->field = implode("\n", $array);

Example:- https://eval.in/867628