Jump to content

MultiDim array (loop)


viviosoft

Recommended Posts

Hello all!

 

I can't seem to wrap my head around how to accomplish my problem.  Here's my problem.  I have several array's (only two this example).

 

<?php
	$item_num =   array ( 0 => "00000", 1 => "00001", 2 => "00002" ),
                $item_disc =    array ( 0 => "Some info for 0", 1 => "Some info for 1", 2 => "Some info for 2" )
?>

 

I want to build a table using the arrayed information.  Something like:

 

<table>

   <tr>
      <td><input value="'.$item_num[0].'" name="item[]" size="9"></input></td>
      <td><input value="$item_disc[0]" name="disc[]" size="33"></input></td> 
   </tr>

   <tr>
      <td><input value="'.$item_num[1].'" name="item[]" size="9"></input></td>
      <td><input value="$item_disc[1]" name="disc[]" size="33"></input></td> 
   </tr>

<table>

 

I know how to loop through an array but this is a bit different in that I each array would need extracted in the loop.  If you notice I want to build rows and the values for each row are in separate arrays.  So $item_num and $item_disc are in the same row and exacts ['0'] and the next row is [1] and the next row is [2], etc...

 

Any help would be great.  Thanks.

 

viviosoft -

 

Link to comment
Share on other sites

Wouldn't a better structure for the array then be:

 

$items = array(
    array(
        "00000",
        "Some info for 0"
    ),
    array(
        "00001",
        "Some info for 1"
    ),
    array(
        "00002",
        "Some info for 2"
    )
);

 

By the way what you had before was not a multi-dimensional array.

Link to comment
Share on other sites

I guess you didn't put any thought into what I said. I was suggesting a better to way to structure the array, that would make looping through it simpler. All you'd have to do with that type of structure is:

 

<table>
    <?php foreach ($items as $item) { ?>
        <tr>
            <td><input value="<?php echo $item[0]; ?>" name="item[]" size="9"></input></td>
            <td><input value="<?php echo $item[1]; ?>" name="disc[]" size="33"></input></td>
        </tr>
    <?php } ?>
</table>

Link to comment
Share on other sites

No, I understood what you said.  I should have said that the information is coming from the database that way.  I'm using serialized data from the database.  Each row in the database table is a built array.  So, array one are items, and array two are the descriptions and so on.  The sample code that I provided was just an example... Does that clarify things a bit?

Link to comment
Share on other sites

Yeah that's the kind of reasoning I was looking for. You could do it this way then:

 

<table>
    <?php foreach ($item_num as $key => $item) { ?>
        <tr>
            <td><input value="<?php echo $item; ?>" name="item[]" size="9"></input></td>
            <td><input value="<?php echo $item_disc[$key]; ?>" name="disc[]" size="33"></input></td>
        </tr>
    <?php } ?>
</table>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.