How do I check if an array already exists?

    [man]isset/man will tell you if the variable is already created, and [man]is_array/man will tell you if it's an array. You can then check to see if it's populated with [man]empty/man.

    So:

    if(isset($array) && is_array($array) && !empty($array))
    {
        // Okay, $array is a non-empty array
    }

      Why doesn't the following code work.

      I'm trying to test if an array exists, to insert data into it, else create the array. Only each time I access the page, the array contains no data. Why is this?

      <?php
      session_start();
      
      $id = $_GET['id'];
      $array = $_SESSION['ids'];
      if (!isset($array))
      {
      $array = array();
      }
      else
      {
      
      $array[] = 'Data';
      $_SESSION['ids'] = $array;
      }
      
      
      echo $_SESSION['ids'];
      
      print_r($array);
      ?>

        It is okay to ask a follow-up question in your previous thread. So I've merged them together. No reason to have 2 threads for essentially one question.

        I'm not sure why it won't work.

          $array is always set because

          $array = $_SESSION['ids'];

          How about:

          session_start();
          if (!isset($_SESSION['ids']))
          {
            $_SESSION['ids'] = array();
          }
          $_SESSION['ids'][] = 'Data';
          
          print_r($_SESSION['ids']);
          
            Write a Reply...