16

I have an array that I want on multiple pages, so I made it a SESSION array. I want to add a series of names and then on another page, I want to be able to use a foreach loop to echo out all the names in that array.

This is the session:

$_SESSION['names']

I want to add a series of names to that array using array_push like this:

array_push($_SESSION['names'],$name);

I am getting this error:

array_push() [function.array-push]: First argument should be an array

Can I use array_push to put multiple values into that array? Or perhaps there is a better, more efficient way of doing what I am trying to achieve?

zeckdude
  • 15,877
  • 43
  • 139
  • 187

6 Answers6

40

Yes, you can. But First argument should be an array.

So, you must do it this way

$_SESSION['names'] = array();
array_push($_SESSION['names'],$name);

Personally I never use array_push as I see no sense in this function. And I just use

$_SESSION['names'][] = $name;
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
12

Try with

if (!isset($_SESSION['names'])) {
    $_SESSION['names'] = array();
}
array_push($_SESSION['names'],$name);
hsz
  • 148,279
  • 62
  • 259
  • 315
1
 $_SESSION['total_elements']=array();
 array_push($_SESSION['total_elements'], $_POST["username"]);
Ravi Mane
  • 1,468
  • 2
  • 18
  • 24
  • 3
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Bono Mar 10 '15 at 16:18
1

Yes! you can use array_push push to a session array and there are ways you can access them as per your requirement.

Basics: array_push takes first two parameters array_push($your_array, 'VALUE_TO_INSERT');. See: php manual for reference.

Example: So first of all your session variable should be an array like:

$arr = array(
    's_var1' => 'var1_value',
    's_var2' => 'var2_value'); // dummy array
$_SESSION['step1'] = $arr;     // session var "step1" now stores array value

Now you can use a foreach loop on $_SESSION['step1']

foreach($_SESSION['step1'] as $key=>$value) {
    // code here
}

The benefit of this code is you can access any array value using the key name for eg:

echo $_SESSION[step1]['s_var1']  // output: var1_value

NOTE: You can also use indexed array for looping like

$arr = array('var1_value', 'var1_value', ....);

BONUS: Suppose you are redirected to a different page You can also insert a session variable in the same array you created. See;

// dummy variables names and values
$_SESSION['step2'] = array(
    's_var3' => 'page2_var1_value',
    's_var4' => 'page2_var2_value');


$_SESSION['step1step2'] = array_merge($_SESSION['step1'], $_SESSION['step2']);

// print the newly created array
echo "<pre>";  // for formatting printed array
var_dump($_SESSION['step1step2']);
echo "<pre>";

OUTPUT:

// values are as per my inputs [use for reference only]
array(4) {
  ["s_var1"]=>
  string(7) "Testing"
  ["s_var2"]=>
  int(4) "2124"
  ["s_var3"]=>
  int(4) "2421"
  ["s_var4"]=>
  string(4) "test"
}

*you can use foreach loop here as above OR get a single session var from the array of session variables.

eg: 
echo $_SESSION[step1step2]['s_var1'];
OUTPUT:
Testing

Hope this helps!

Erielama
  • 51
  • 1
  • 5
0
<?php
session_start();

$_SESSION['data']= array();
$details1=array('pappu','10');
$details2=array('tippu','12');

array_push($_SESSION['data'],$details1);
array_push($_SESSION['data'],$details2);

foreach ($_SESSION['data'] as $eacharray) 
{
 while (list(, $value) = each ($eacharray)) 
    {
        echo "Value: $value<br>\n";
    }
}
?>

output

Value: pappu
Value: 10
Value: tippu
Value: 12

Praveen Srinivasan
  • 1,562
  • 4
  • 25
  • 52
internals-in
  • 4,798
  • 2
  • 21
  • 38
0

Try this, it's going to work :

session_start();

if(!isset($_POST["submit"]))
{
    $_SESSION["abc"] = array("C", "C++", "JAVA", "C#", "PHP");
}

if(isset($_POST["submit"]))
{
    $aa = $_POST['text1'];

    array_push($_SESSION["abc"], $aa);

    foreach($_SESSION["abc"] as $key => $val)
    { 
        echo $val;
    }
}
Sergiu Paraschiv
  • 9,929
  • 5
  • 36
  • 47