get keys of multi level array

Discussion in 'PHP' started by ernest1a, Jun 21, 2009.

  1. #1
    I am wondering what I am doing wrong in the following code, because I get error:
    Warning: Illegal offset type in isset or empty in D:\xampp\htdocs\testing\4.php on line 5

    
    $_POST[firstkey][secondkey]=12;
    if (isset($_POST)) {
      foreach($_POST as $key){
        foreach ($_POST[$key] as $key2 => $value){
        echo "<br />Key=".$key2."value=".$value;
        }
      }
    }
    
    PHP:
    How would be the correct was echo in the same statement 1st and 2nd key and value?

    Thank you
     
    ernest1a, Jun 21, 2009 IP
  2. JDevereux

    JDevereux Peon

    Messages:
    50
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think you want something like this:

    $_POST[firstkey][secondkey]=12;
    if (isset($_POST)) {
      foreach($_POST as $key=>$value){
        if (is_array($value)) {
           foreach ($value as $key2 => $value2){
              echo "<br />First Key= ".$key. "Second Key=".$key2." value=".$value2;
          }
        }
        else {
          echo "<br />Key=".$key." value=".$value;
          }
        }
    }
    PHP:
     
    JDevereux, Jun 21, 2009 IP