1

Sorry to ask if it was already asked before, but none of those solutions worked for me. Everytime I try to add into array, what I get is new array. See PHP code below:

<?php    
session_start();
if (!isset($_SESSION['$rNumber'])){
    $_SESSION['$rNumber'] = 0;
}

if(empty($_SESSION['words']))
{
    $_SESSION['words'] = array();
}

if (isset($_POST['button1'])){
    $random = rand(10, 20);
    $_SESSION['$rNumber'] = $_SESSION['$rNumber'] + $random;
    $word  = 'You entered a farm and earned '.$random.' golds.';
    array_push($_SESSION['words'], $word);
} else if ...

}
?>

When I var_dump the $words[], it always has 1 value only, though I add this very many times! Please let me know if additional info needed. Thanks!

Sam
  • 900
  • 10
  • 18
  • 1
    note that `'$rNumber'` is going to put an entry in your array where the key is the literal characters `$`, `r`, `N`, etc... – Marc B Apr 16 '15 at 18:16
  • @JayBlanchard: what good would that do? `[]` is the push operator, and as written you'd be pushing nothing because it has to be `$arr[] = $something`. – Marc B Apr 16 '15 at 18:16
  • I am curious why you expect `$_SESSION['words'][]` to have more than one entry? No loop, so I am guessing you want it to add a new element to the array each time something is Posted? – Twisty Apr 16 '15 at 18:25
  • Why not: `$_SESSION['$rNumber'] += $random;` - not addressing your issue, but makes life a bit nicer. – Twisty Apr 16 '15 at 18:27
  • I think you are trying to generate a random number every time the button is clicked and store those random numbers in array. If I am right, what are you doing with sessions here? Are you submitting the form? – The Guest Apr 16 '15 at 18:28
  • @SamSB Im just wandering who had the right answer for your question? Can you please check the answer as right - i mean the one that helped you :-) – Redrif Apr 21 '15 at 01:32

4 Answers4

1

You are inspecting the wrong variable with var_dump() So try to var_dump($_SESSION['words']) and not var_dump($words). I tried it and it works for me. Array push adds the value $word into $_SESSION['words'], therefore you have to var_dump the $_SESSION['words'] because $word is still just a string, not an array.

vascowhite
  • 18,120
  • 9
  • 61
  • 77
Redrif
  • 650
  • 4
  • 22
0

You could also do it like this:

$_SESSION['words'][] = 'You entered a farm and earned '.$random.' golds.';

instead of: $word = 'You entered a farm and earned '.$random.' golds.'; array_push($_SESSION['words'], $word);

UPDATE: From php.net: Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.

Armand
  • 215
  • 1
  • 6
0

thank you for your suggestions. I made these changes and it worked!

<?php

session_start();
if (!isset($_SESSION['$rNumber'])){
    $_SESSION['$rNumber'] = 0;
}

if(empty($_SESSION['words']))
{
    $_SESSION['words'] = array();
}


if (isset($_POST['button1'])){
    $random = rand(10, 20);
    $_SESSION['$rNumber'] = $_SESSION['$rNumber'] + $random;
    $word  = 'You entered a farm and earned '.$random.' golds.';
    array_push($_SESSION['words'], $word);
} else if...
Sam
  • 900
  • 10
  • 18
-1

A few issues, as @MarcB pointed out, you should have a set Index name for your SESSION variable.

<?php    
session_start();
if (!isset($_SESSION['gold'])){
    $_SESSION['gold'] = 0;
}

if(empty($_SESSION['words']))
{
    $_SESSION['words'] = array();
}

if (isset($_POST['button1'])){
    $random = rand(10, 20);
    $_SESSION['gold'] += $random;
    $word = "You entered a farm and earned $random golds.";
    $_SESSION['words'][] = $word;
}
?>
Twisty
  • 30,304
  • 2
  • 26
  • 45