-2

So I have declared a Session array and initialized it with zeroes. It's basically a multidimensional array. However, I'm thinking of converting it to a regular array because everytime I test whether a value exists or not using the in_array() function, it fails. It keeps adding existing values.

<?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"];
        for($r = 0; $r<sizeof($_SESSION['numbers']); $r++){
            for($c = 0; $c<sizeof($_SESSION['numbers']); $c++){
                $colVal = $_SESSION['numbers'][$r][$c];
                    insertInputAt($r,$c,$userInput);    
            }
        }
    }

    function insertInputAt($row,$col,$input){
        if(!in_array($input, $_SESSION['numbers'])){ //this fails
            echo $input . "<br/>";
            $_SESSION['numbers'][$row][$col] = $input;
        }
    }
?>

If I enter lets say 5, it inserts the input 5 to all rows and columns. I get 25 echos of value of 5 even if I put a !in_array() condition

I thought maybe if I parse the $_SESSION['numbers] as a regular array within the insertInputAt() method, the !in_array() condition might work accurately.

Thank you.

heisenberg
  • 1,784
  • 4
  • 33
  • 62
  • If you are going to use `in_array` you will need to select to correct "child" array with your `$row` param: `in_array($input, $_SESSION['numbers'][$ow])` – DarkBee Nov 27 '17 at 09:06
  • `$r` & `$c` would have `0,1,2,3,4`. Also `in_array()` wont work for multidimensional `array`s – Sougata Bose Nov 27 '17 at 09:07
  • 1
    Possible duplicate of [Inserting and replacing values to multidimensional array when it exists or not](https://stackoverflow.com/questions/47495969/inserting-and-replacing-values-to-multidimensional-array-when-it-exists-or-not) – splash58 Nov 27 '17 at 09:15

2 Answers2

1

Modify your insertInputAt function to this:

function insertInputAt($row,$col,$input){
    if(!in_array($input, $_SESSION['numbers'][$row])){ //this fails
        echo $input . "<br/>";
        $_SESSION['numbers'][$row][$col] = $input;
    }
}
Aman Maurya
  • 1,305
  • 12
  • 26
0

First of all, you do not need to initialize $_SESSION['numbers'].

<?php 
session_start();
$userInput = $_POST["num"] = 1;
for($r=0;$r<count($_SESSION['numbers']);$r++){
    $found = 0;
    for($c=0;$c<count($_SESSION['numbers'][$r]);$c++){
        if(($_SESSION['numbers'][$r][$c]==0)&&(myfunction($_SESSION['numbers'],$userInput)==0)){
            $_SESSION['numbers'][$r][$c] = $userInput;unset($_POST['num']); $found=1;break;
        }
    }
    if($found==1)break;
}

function myfunction($array,$value){
    foreach($array as $q){
        if(!in_array($value,$q)){
            for($i=0;$i<count($q);$i++){
                if($q[$i]==0) return false;
            }
        }
    }
}
echo "<pre>";print_r($_SESSION['numbers']);
?>
GYaN
  • 2,327
  • 4
  • 19
  • 39