I am trying to replace the initial values of a multidimensional session array everytime the user submits a form. It must not have a duplicate in the array. I'm having trouble inserting user input value per form submission to specific row and column of the session array.
All codes are in one php file.
<?php
session_start();
$_SESSION['numbers'] = array(
array(0,0,0,0,0), //row1
array(0,0,0,0,0), //row2
array(0,0,0,0,0), //row3
array(0,0,0,0,0), //row4
array(0,0,0,0,0) //row5
);
?>
<?php
if (isset($_POST["num"]) && !empty($_POST["num"])){
$userInput = $_POST["num"];
if(!in_array($userInput, $_SESSION['numbers'])){
echo "<script>alert('does not exist')</script>";
//$_SESSION['numbers'] [] = $userInput;
}else{
echo "<script>alert('exists')</script>";
//don't add to array
}
}
echo "<table border = 1>";
for($row = 0; $row < sizeof($_SESSION['numbers']); $row++){
echo "<tr>";
for($col = 0; $col < sizeof($_SESSION['numbers']); $col++){
echo "<td>".$_SESSION['numbers'][$row][$col]."</td>";
}
echo "</tr>";
}
echo "</table>";
?>
However, if I do
$_SESSION['numbers'] = $userInput;
it replaces the entire multidimensional array with just 1 value instead of inserting it to the specific row and column of the nested array.
If input is, lets say 1 and the user hits the submit button, the output I get in table for each row and column is just 1
I can't properly insert it in a specific row<tr>
and column<td>
.
It's easy if it's one dimensional array but I don't know how to do it in multidimensional arrays nested. Please help.
Thank you.