foreach and multiple arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dudelideisann
    New Member
    • Jan 2008
    • 12

    foreach and multiple arrays

    How can I output this one correctly through a foreach-loop?

    [PHP]Array
    (
    [0] => Array
    (
    [Table] => Asdf
    [Field] => Array
    (
    [0] => asdf
    )

    [Values] => Array
    (
    [0] => asdf
    )

    )

    [1] => Array
    (
    [Table] => SomeTablename
    [Field] => Array
    (
    [0] => some subject
    )

    [Value] => Array
    (
    [0] => 298282
    )

    )

    )[/PHP]

    With this one:


    [PHP]foreach($_SESSI ON['sessionName] as $table) {
    foreach($table as $key => $value) {

    echo "<tr><td>$k ey</td><td>$value</td>";


    }

    }[/PHP]


    I only get this output:

    Table Asdf
    Field Array
    Value Array
    Table SomeTableName
    Field Array
    Value Array

    Why the Array and not my values inside this array?
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Because you must iterate again when the value is an array. Usually you set up your array print routine such that it can re-invoke itself. You have not mentioned what and how you would like to print it out, but the following will give you an idea of iteration of your array[php]get_array_elems ($yourarray);

    function get_array_elems ($arrResult, $where="array") {
    reset($arrResul t);
    while(list($key ,$value)=each($ arrResult)) {
    if (is_array($valu e)){
    get_array_elems ($value, $where."[$key]");
    }
    else {
    for ($i=0; $i<count($value );$i++) {
    echo $where."[$key]=".$value."<br> ";
    } // Eind FOR
    } // Eind ELSE
    } // Eind WHILE
    } // Eind FUNCTION[/php]It will output
    Code:
    array[0][Table]=Asdf
    array[0][Field][0]=asdf
    array[0][Values][0]=asdf
    array[1][Table]=SomeTablename
    array[1][Field][0]=some subject
    array[1][Values][0]=298282
    Ronald

    Comment

    • dudelideisann
      New Member
      • Jan 2008
      • 12

      #3
      Originally posted by ronverdonk
      Because you must iterate again when the value is an array. Usually you set up your array print routine such that it can re-invoke itself. You have not mentioned what and how you would like to print it out, but the following will give you an idea of iteration of your array[php]get_array_elems ($yourarray);

      function get_array_elems ($arrResult, $where="array") {
      reset($arrResul t);
      while(list($key ,$value)=each($ arrResult)) {
      if (is_array($valu e)){
      get_array_elems ($value, $where."[$key]");
      }
      else {
      for ($i=0; $i<count($value );$i++) {
      echo $where."[$key]=".$value."<br> ";
      } // Eind FOR
      } // Eind ELSE
      } // Eind WHILE
      } // Eind FUNCTION[/php]It will output
      Code:
      array[0][Table]=Asdf
      array[0][Field][0]=asdf
      array[0][Values][0]=asdf
      array[1][Table]=SomeTablename
      array[1][Field][0]=some subject
      array[1][Values][0]=298282
      Ronald

      Thanks! That's great. How can I make it appear as a table with two cells and without the index numbers [0] and [1] and so on..?

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        ....without the index numbers [0] and [1] and so on..?....
        I do not understand that question fully. Please draw it.

        Ronald

        Comment

        • dudelideisann
          New Member
          • Jan 2008
          • 12

          #5
          Originally posted by ronverdonk
          I do not understand that question fully. Please draw it.

          Ronald
          Hi !

          I would like the output to be like this (without the bold index-numbers that come along)
          [HTML]<table>
          <tr>
          <td>0Tablename</td><td>=Test</td></tr>
          <tr>
          <td>0Subject0</td><td>=test</td></tr>
          <tr>
          <td>0Subject1</td><td>=test</td></tr>
          <tr>
          <td>0Subject2</td><td>=test</td></tr>
          <tr>
          <td>0Value0</td><td>=sometex t</td></tr>
          <tr>
          <td>0Value1</td><td>=someoth ertext</td></tr>
          <tr>
          <td>0Value2</td><td>=someval ue</td></tr>
          <tr>
          <td>1Tablename</td><td>=Testaa</td></tr>
          <tr>
          <td>1Subject0</td><td>=asdf</td></tr>
          <tr>
          <td>1Value0</td><td>=sometex t</td></tr>

          <!-- and so on until all is out and then : -->

          </table>
          [/HTML]

          Anyway, thank you so much for your help!

          Comment

          Working...