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!

6
  • 1
    note that '$rNumber' is going to put an entry in your array where the key is the literal characters $, r, N, etc... Commented Apr 16, 2015 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. Commented Apr 16, 2015 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? Commented Apr 16, 2015 at 18:25
  • Why not: $_SESSION['$rNumber'] += $random; - not addressing your issue, but makes life a bit nicer. Commented Apr 16, 2015 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? Commented Apr 16, 2015 at 18:28

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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...

Comments

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.

Comments

-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;
}
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.