Search code examples
phparraysexplodeimplode

how to put comma separated words into array


I have an array which i have converted into comma separated here is how I did it:

$array[] = $imp;
$strings = implode(", ", $array);

After implode I get 34, 56, 78.

Now I have an array stored in session and I want to add $strings into it like:

array_push($_SESSION['array'],$strings);

But when printed I get:

Array ( [0] => 191 [2] => 34, 56, 78 )

I want to add 34, 56, 78 value separately so that array can look like this:

Array ( [0] => 191 [2] => 34 [3] => 56 [4] => 78 )

Solution

  • Copy-paste next code and run in your browser :

    <html>
      <head>
      </head>
      <body>
    <?php
    $_SESSION[ "arr" ] = array( "100","200","300" );
    $strings = "34,56,78";
    $_SESSION[ "arr" ] = array_merge( $_SESSION[ "arr" ],explode( ",",$strings) );
    var_dump( $_SESSION[ "arr" ] );
    ?>
      </body>
    </html>