Dan Dan - 13 days ago 5
PHP Question

Add <br /> after 3rd symbol in Array

I have the following variable:

$checkbox = implode(';', $_POST['product']);


$checkbox is equal to "Product Name;Price;Unit", how can I add a break after every line?

At the moment $checkbox is equal to:

ASFP4040;18.95;1;ASFP4048;21;1;ASGS100100;25.45;1


I need it to be like:

ASFP4040;18.95;1;
ASFP4048;21;1;
ASGS100100;25.45;1;


EDIT:

I am writing this to a .TXT file, \n shows as text and doesn't actually create a new line.

Answer

As I'm not sure, how your $_POST['products'] var looks like, you might one of this options

If you have everything in a single array element like this

Array
(
    [0] => ASFP4040
    [1] => 18.95
    [2] => 1
    [3] => ASFP4048
    [4] => 21
    [5] => 1
    [6] => ASGS100100
    [7] => 25.45
    [8] => 1
)

you could split the array into chunks and join them together

$data = implode("\n", array_map(function($chunk) {
    return implode(';', $chunk);
}, array_chunk($_POST['product'], 3))) . ';';

If you hast have

Array
(
    [0] => ASFP4040;18.95;1
    [1] => ASFP4048;21;1
    [2] => ASGS100100;25.45;1
)

a simple implode would be enough

$data = implode("\n", $_POST['product']);
Comments