Warning: in_array() expects parameter 2 to be array, null given

前端 未结 4 1701
悲&欢浪女
悲&欢浪女 2021-01-27 03:47

I am getting the following warning when trying to add data to a session (and checking if it already exists).

Warning: in_array() expects parameter 2 to be

相关标签:
4条回答
  • 2021-01-27 04:09

    check if the value is set before use it with isset and use is_array to check if a given variable is an array.

    if(isset($_GET['product']) && is_set($_SESSION['product']) && is_array($_SESSION['product']) && !in_array($_GET['product'], $_SESSION['product'])){
        $_SESSION['product'][] = $_GET['product'];
    }
    
    0 讨论(0)
  • 2021-01-27 04:10

    Your $_SESSION['product'] is empty. Try this,

    if(!empty($_SESSION['product']) && isset($_GET['product']) && !in_array($_GET['product'], $_SESSION['product'])){
        $_SESSION['product'][] = $_GET['product'];
    }
    

    It should work.

    0 讨论(0)
  • 2021-01-27 04:17

    The warining says it all. This param is null:

     $_SESSION['product']
    

    Make sure it is set before you use it. Example:

    if(isset($_SESSION['product']) && isset($_GET['product']) &&  !in_array($_GET['product'], $_SESSION['product'])){
            $_SESSION['product'][] = $_GET['product'];
        }
    
    0 讨论(0)
  • 2021-01-27 04:26

    you should always apply check for array

    isset( $_SESSION['product']) in your is condition before & condition 
    
    0 讨论(0)
提交回复
热议问题