0

I have the ff code which stores values inputted in form's textfield to a session array which I named "numbers". I need to display the value of the array but everytime I try echo $value; I get an error Array to string conversion in

I used echo var_dump($value); and verified that all the inputted values are stored to the session array.

My goal is to store the user input to an array everytime the user hits the submit button.

How do I correct this?

<?php 
    session_start();
?>

<html>
<head>
    <title></title>
</head>
<body>
    <form method="POST" action="index.php"> 
        <label>Enter a number</label>&nbsp;
        <input type="text" name="num" required />
         <button type="submit">Submit</button> 
    </form>
</body>
</html>

    <?php
    if (isset($_POST["num"]) && !empty($_POST["num"])){
        $_SESSION['numbers'][] = $_POST["num"];

        foreach($_SESSION as $key => $value){
            echo ($value);
        }
    }
    ?>

Thank you.

3
  • $value is an array. You should have it as $_SESSION['numbers'] = $_POST["num"];.. You don't need to empty [] Commented Nov 26, 2017 at 8:23
  • You are using $_SESSION['numbers'][] = $_POST["num"]; which creates another array level Commented Nov 26, 2017 at 8:23
  • adding [] makes a variable array. So when u try to print an array with Echo it will give error Commented Nov 26, 2017 at 8:25

3 Answers 3

1

When doing $_SESSION['numbers'][] = $_POST["num"];, you are making $_SESSION['numbers'] an array: so you'll either need to do that differently, or check whether $value within your foreach loop is an array or not.

if (isset($_POST["num"]) && !empty($_POST["num"])){
    $_SESSION['numbers'][] = $_POST["num"];

    foreach($_SESSION as $key => $value){
        if (is_array($value)) {
            foreach ($value as $valueNested) {
                echo ($valueNested);
            }
        } else {
            echo ($value);
        }
    }
}

OR

if (isset($_POST["num"]) && !empty($_POST["num"])){
    $_SESSION['numbers'] = $_POST["num"];

    foreach($_SESSION as $key => $value){
        echo ($value);
    }
}

The latter is probably what you are actually trying to accomplish.

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

Comments

0

If you want to echo all entered numbers, your for each cycle should be:

foreach($_SESSION[‘numbers’] as $key => $value) {
    echo $value;
}

This is because the $_SESSION[‘numbers’] itself is the array that contains the numbers.

2 Comments

thank you this solved it. I needed to see what values I get because I'll need to test each value with an if statement to see if it exists? can I use in_array($array) for super global $SESSION?
Yes, I think that you can use in_array
0

The error is because SESSION[“numbers”] is an array and you can just echo an array. It will throw an error “Array to string converstion”.

Iterate through the array and print it instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.