SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Output a Multidimensional Array

    I can't seem to output this array properly.

    PHP Code:
    Array
    (
        [
    0] => Array
            (
                [
    id] => 13
                
    [category_id] => 7
                
    [name] => Leaving Of Liverpool
                
    [description] => Leaving Of Liverpool
                
    [price] => 1.00
                
    [virtual] => 1
                
    [active] => 1
                
    [sort_order] => 13
                
    [created] => 2007-06-24 14:08:03
                
    [modified] => 2007-06-24 14:08:03
                
    [image] => NONE
            
    )

        [
    1] => Array
            (
                [
    id] => 16
                
    [category_id] => 7
                
    [name] => Yellow Submarine
                
    [description] => Yellow Submarine
                
    [price] => 1.00
                
    [virtual] => 1
                
    [active] => 1
                
    [sort_order] => 16
                
    [created] => 2007-06-24 14:10:02
                
    [modified] => 2007-06-24 14:10:02
                
    [image] => NONE
            
    )


    this was my attempt to output the array:
    PHP Code:
    <table>
    <?php 
    foreach($products as $key=>$value){
        echo
    '<tr>';
        foreach(
    $value as $k => $v){
            echo 
    '<td>'  $value '</td>';
        }
        echo 
    '</tr>';
    }
    ?>
    </table>
    but not getting the desired result.

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    $value is just an Array. The data is held in $k and $v. This is probably what you want:
    PHP Code:
    <table>
    <?php 
    foreach($products as $key=>$value){

        foreach(
    $value as $k => $v){
          echo
    '<tr>';
          echo 
    '<td>'  $k '</td>';
          echo 
    '<td>'  $v '</td>';
          echo 
    '</tr>';
        }
    }
    ?>
    </table>

  3. #3
    SitePoint Wizard HarryR's Avatar
    Join Date
    Dec 2004
    Location
    London, UK
    Posts
    1,376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code PHP:
    <table>
      <?php foreach( $products AS $product ): ?>
        <tr>
          <?php foreach( $product AS $key => $value ): ?>
           <td><?php echo "$key = $value"; ?></td>
          <?php endforeach; ?>
        </tr>
      <?php endforeach; ?>
    </table>


    Note that you're looping through the array of products, and in this case just looping again for each product to display it's values, you can however remove the inner loop and display it how you choose, for example:

    Code PHP:
    <table>
      <?php foreach( $products AS $product ): ?>
        <tr>
          <?php echo $product['name'] ?> costs $<?php echo $product['price'] ?>
        </tr>
      <?php endforeach; ?>
    </table>

  4. #4
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    $value is just an Array. The data is held in $k and $v. This is probably what you want:
    PHP Code:
    <table>
    <?php 
    foreach($products as $key=>$value){

        foreach(
    $value as $k => $v){
          echo
    '<tr>';
          echo 
    '<td>'  $k '</td>';
          echo 
    '<td>'  $v '</td>';
          echo 
    '</tr>';
        }
    }
    ?>
    </table>
    hhhmmm... this produces the following generated source:
    <table>
    <tr>
    <td>0</td>
    <td>Array</td>
    </tr>
    <tr>
    <td>1</td>
    <td>Array</td>
    </tr>
    </table>

  5. #5
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by HarryR View Post
    Code PHP:
    <table>
      <?php foreach( $products AS $product ): ?>
        <tr>
          <?php foreach( $product AS $key => $value ): ?>
           <td><?php echo "$key = $value"; ?></td>
          <?php endforeach; ?>
        </tr>
      <?php endforeach; ?>
    </table>

    produces:
    <table>
    <tr>
    <td>0 = Array</td>

    <td>1 = Array</td>
    </tr>
    </table>

    Code PHP:
    <table>
      <?php foreach( $products AS $product ): ?>
        <tr>
          <?php echo $product['name'] ?> costs $<?php echo $product['price'] ?>
        </tr>
      <?php endforeach; ?>
    </table>
    this one produces a syntax error.

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You sure? According to the array output you posted in the first post, $products is the multidimensional array. Then $key is 0 or 1, $value is the sub-array, $k is id, category_id, etc., and $v is 13, 7, Leaving of Liverpool, etc.

    So I don't see how you could have ended up with that, unless you've missed out one of the loops.

  7. #7
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    You sure? According to the array output you posted in the first post, $products is the multidimensional array. Then $key is 0 or 1, $value is the sub-array, $k is id, category_id, etc., and $v is 13, 7, Leaving of Liverpool, etc.

    So I don't see how you could have ended up with that, unless you've missed out one of the loops.
    it's odd, i copied and pasted the exact code you posted.

  8. #8
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Are you also sure that $products is the outer array (i.e. it only contains two arrays)? I'm baffled by this. Try again with what Harry posted, since you don't need the keys of the outer array. Or this, which is the same thing:
    PHP Code:
    <table>
    <?php 
    foreach($products as $product){
        foreach(
    $product as $key => $value){
          echo
    '<tr>';
          echo 
    '<td>'  $key '</td>';
          echo 
    '<td>'  $value '</td>';
          echo 
    '</tr>';
        }
    }
    ?>
    </table>

  9. #9
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    Are you also sure that $products is the outer array (i.e. it only contains two arrays)? I'm baffled by this. Try again with what Harry posted, since you don't need the keys of the outer array. Or this, which is the same thing:
    PHP Code:
    <table>
    <?php 
    foreach($products as $product){
        foreach(
    $product as $key => $value){
          echo
    '<tr>';
          echo 
    '<td>'  $key '</td>';
          echo 
    '<td>'  $value '</td>';
          echo 
    '</tr>';
        }
    }
    ?>
    </table>
    thank god, found the error. was because of another class. thanks a lot man for the help. i was going crazy too.

  10. #10
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    No worries.

  11. #11
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    No worries.
    in an array, do you have to output all values of keys, or can i choose which one's in my array get outputted?

    if i can't, i know how i can fix otherwise.

  12. #12
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yeah, you can choose which ones. Look at the second block of code HarryR posted above.


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
  •