Concatenate numerical values in an array using a hyphen

0
 <?php

$tipo = $_POST["radatenua"];

if ($tipo > 0) {

    if ($tipo == 40) {

        $atenuantes = $_POST["atenuantes"];
        foreach ($atenuantes as $at):
            $atenuante1 .= $at . "-";
        endforeach;

    }

    elseif ($tipo == 41) {
        $agravantes = $_POST["agravantes"];
        foreach ($agravantes as $ag):
            $agravante1 .= $at;
        endforeach;
    }

}
?> 

I have this code where depending on a radiobutton it makes a certain saved in the database, it is a system that is not mine, therefore there is an error in the database, and then they ask me to save the different extenuating or aggravating in a certain field, that is, you should save something like this: 1-2, 7-8, 1-1. because then a specific code cuts that array .

How could I modify my code to keep it that way?

Currently keeps them as follows: 56 78 1011

    
asked by Victor Alvarado 10.02.2017 в 15:05
source

1 answer

5

You can use the implode () PHP function.

implode () , performs a separation of the array and "pastes" indicating which character you want to join, in this case with a script.

<?php

$arreglo = array(56, 78, 1001);

print implode("-", $arreglo);

?>
  

Result

php prueba.php 
56-78-1001

More information: implode ()

As in your case $atenuantes is the fix, you could do this:

$agravante1 = implode("-", $atenuantes);
    
answered by 10.02.2017 / 15:11
source