-2

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 )
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
Aniket Singh
  • 857
  • 4
  • 16
  • 39

3 Answers3

3

Why did you implode the array if you want an array? Just do this:

$_SESSION['array'] = array_merge($_SESSION['array'], $imp);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

Here is solution for you.

<?php
$arr= array('5','10,20,30');
$newArr = [];
foreach ($arr as $key => $value) {    
    $element = explode(',',$value);
    if (count($element)>1) {
      echo "This is an array";
      foreach ($element as $key => $value1) {
        $newArr[]=$value1;
      }   
    } else {
      echo "Not an array";
      $newArr[]=$value;
    }
}
echo "<pre>";print_r($newArr);

?>
Sachin I
  • 1,500
  • 3
  • 10
  • 29
0

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>