viviosoft Posted March 30, 2011 Share Posted March 30, 2011 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 - Quote Link to comment https://forums.phpfreaks.com/topic/232166-multidim-array-loop/ Share on other sites More sharing options...
Adam Posted March 30, 2011 Share Posted March 30, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/232166-multidim-array-loop/#findComment-1194280 Share on other sites More sharing options...
viviosoft Posted March 30, 2011 Author Share Posted March 30, 2011 It would be. How is that solving the question I have though? How would I build the table using either array method? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/232166-multidim-array-loop/#findComment-1194283 Share on other sites More sharing options...
Adam Posted March 30, 2011 Share Posted March 30, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/232166-multidim-array-loop/#findComment-1194288 Share on other sites More sharing options...
viviosoft Posted March 30, 2011 Author Share Posted March 30, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/232166-multidim-array-loop/#findComment-1194293 Share on other sites More sharing options...
Adam Posted March 30, 2011 Share Posted March 30, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/232166-multidim-array-loop/#findComment-1194295 Share on other sites More sharing options...
viviosoft Posted March 30, 2011 Author Share Posted March 30, 2011 Thanks MrAdam, That's what I needed. Sorry for the confusion. Quote Link to comment https://forums.phpfreaks.com/topic/232166-multidim-array-loop/#findComment-1194300 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.