Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8612295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:35:29+00:00 2026-06-12T04:35:29+00:00

I have a table: | header_row | cell_usr_a | cell_usr_b | cell_fixed_a | cell_fixed_b

  • 0

I have a table:

| header_row | cell_usr_a | cell_usr_b | cell_fixed_a | cell_fixed_b |
| second_row |...
| usr_row_a  |...
| usr_row_b  |...
| usr_row_c  |...

The first two columns in this example (cell_usr*) come from a dynamic object (an array of x entries); there could be 0 or many of those; the last two columns are always there.

The first two rows are always there (element name, and other general properties).
The usr_row* depend on elements in the dynamic object: if one of the element of the array has the correspondent property set, I show the row, otherwise I don’t.

How can I efficiently iterate through the object without having to re-iterate for every row?

Currently I do

foreach ($array as $element)

for every row, adding according to the size of $array.
Then I would need to first iterate through all the $element again to see if the usr_row property is set, and if yes, construct the row…

Any idea on how to do this efficiently (and possibly elegantly)?
It does not necessarily to be a table by the way.

EDIT: some more code to help illustrate my question
$packages is the object, it’s basically an array with unknown number of elements

 <table>
    <tr><!-- header row -->
             <td>Package name</td>
            <?php foreach($packages as $pkg) {
               echo '<td>'.$pkg->package_name . '</td>';
            } ?>
            <td>fixed value</td><!-- cell_fixed_a -->
            <td>no value</td><!-- cell_fixed_b -->
          </tr>
          <tr><!-- second row -->
            <td>Count</td>
            <?php foreach($packages as $pkg) {
               echo '<td>'.$pkg->count . '</td>';
            } ?>
            <td>fixed value</td><!-- cell_fixed_a -->
            <td>fixed value</td><!-- cell_fixed_b -->
          </tr>
          <tr><!-- usr_row_a 
               HERE I ONLY NEED TO HAVE THIS ROW IF any one of $pkg has prop set
               -->
            <?php foreach($packages as $pkg) {
              if ($pkg->prop) {
                echo '<tr><td>Property name</td>';
                echo '<td>'. $pkg->prop . '</td>';
            }
         </tr>
        </table>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:35:30+00:00Added an answer on June 12, 2026 at 4:35 am

    You don’t need to loop through $packages 3 times .. you can just save the result in variables

    $name = "";
    $count = "";
    $property = "";
    
    foreach ( $packages as $pkg ) {
        $name .= '<td>' . $pkg->package_name . '</td>';
        $count .= '<td>' . $pkg->count . '</td>';
    
        if ($pkg->prop) {
            $property .= '<tr><td>Property name</td>';
            $property .= '<td>' . $pkg->prop . '</td>';
        }
    }
    

    you can echo them is respective places …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a table of mine I have the table header, th which have two
I have this bit of JavaScript... 15 $('.ajax_edit_address').each(function() { 16 $(this).ajaxForm({ 17 target: $(this).parents('table.address').find('tr.address_header').children(':first'),
I have an html table with one of the headers spanning over 2 columns.
I have a table like this: <table> <tr> <th>Header</th> <td>Content</td> </tr> <tr> <th>Header</th> <td>Content</td>
I have an HTML table in which the first column's cells each contain a
I have a table that looks something like this: <table> <tr id=header> <th>Title</th> <th>Title</th>
I have a grouped table with 3 categories and one header above the first
I have a html table structure like this; <tr style=font-weight: bold> <td>ID</td> <td>Navn</td> <td>Adresse</td>
I would like to have a table header that looks like the image shown
I have a table with a header and an empty body: <table style=width:50%; class=adminlist>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.