SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Feb 2007
    Posts
    230
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    get keys of multi level array

    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

    PHP Code:
    $_POST[firstkey][secondkey]=12;
    if (isset(
    $_POST)) {
      foreach(
    $_POST as $key){
        foreach (
    $_POST[$key] as $key2 => $value){
        echo 
    "<br />Key=".$key2."value=".$value;
        }
      }

    How would be the correct was echo in the same statement 1st and 2nd key and value?

    Thank you
    Dilster.com - Dating blog where you can find new dating tips, advices and ideas or your partner.

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    Foreach($SomeArray as $Item){ 
    Means that for every item in somearray, $Item is given that VALUE

    PHP Code:
    Foreach($SomeArray as $Key => $Item){ 
    Means that for every item in somearray, $Key is given the Key, Item is given the VALUE

    Can you see your error now?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2009
    Posts
    144
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but how I can get 1st and 2nd key and value for each $_POST? I have no idea.

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $_POST['firstkey']['secondkey']=12;
    if (!empty(
    $_POST)) {
        foreach(
    $_POST as $Key => $Array){
            
    PrintF('<h1>%s</h1>'$Key);
            echo 
    '<ul>';
            foreach (
    $Array as $SubKey => $Value){
                
    PrintF('<li>
                             <ul>
                                 <li>Key:     %s</li>
                                 <li>Value:  %s</li>
                             </ul>
                         </li>'
                    
    $SubKey
                    
    $Value
                
    );
            }
            echo 
    '</ul>';
        }

    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •