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 7922249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:53:27+00:00 2026-06-03T16:53:27+00:00

A simple Google search will reveal a multitude of solutions for converting a two

  • 0

A simple Google search will reveal a multitude of solutions for converting a two dimension array into HTML using PHP. Unfortunately none of these has the answers I am looking for.

I want a generic piece of code that converts an array into a HTML table. Where most examples go wrong is that they assume the programmer knows the name of the table’s fields . I want this code to be generic such that I can use it even if I do not know the name of the fields.

I can see I need two loops. One nested inside of the other. What I am not sure of is how to get the values out given I don’t know the keys.

The end result will hopefully output html something like this:

<th>
  <td>x/y</td>
  <td> x1 </td>
  <td> x2 </td>
</th>
<tr>
  <td>y1</td>
  <td> x1y1 </td>
  <td> x2y1 </td>
</tr>
<tr>
  <td>y2</td>
  <td> x1y2 </td>
  <td> x2y2 </td>
</tr>

Please remember I want a generic and simple solution. I hope this is clear.

  • 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-03T16:53:27+00:00Added an answer on June 3, 2026 at 4:53 pm

    The following code will look through the two dimensions of the array and make them into a table. Regardless of what the key may be, you will get a visual representation of it. If they do have key name and not just an index, the values will be available in $key and $subkey respectively. So you have them if you need them.

    The code:

    $myarray = array("key1"=>array(1,2,3,4),
                     "key2"=>array(2,3,4,5),
                     "key3"=>array(3,4,5,6),
                     "key4"=>array(4,5,6,7)); //Having a key or not doesn't break it
    $out  = "";
    $out .= "<table>";
    foreach($myarray as $key => $element){
        $out .= "<tr>";
        foreach($element as $subkey => $subelement){
            $out .= "<td>$subelement</td>";
        }
        $out .= "</tr>";
    }
    $out .= "</table>";
    
    echo $out;
    

    The result:

    enter image description here

    If you want to see the keys as headings, you could add this code after the echo "<table>"; line:

    echo "<tr>";
    foreach($myarray as $key => $element) echo "<td>$key</td>";
    echo "</tr>";
    

    Resulting in this:

    enter image description here

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

Sidebar

Related Questions

I'm trying to write a simple google extension that will upon clicking ctrl+alt+x search
A simple google search for: CGRect + C or CGRect in C brings back
I have a simple Google App Engine app, that I wrote using ordinary strings.
I'm building a simple appliacation using the Google Maps API and I'm trying to
Using Google + Bing didn't yield an answer to what should be a simple
Just as simple as this, I need to search an array that is binded
I have two JSON objects here, generated through the Google Search API. The URL's
Simple true or false: Having Google Analytics installed on your page will automatically cause
I want to implement a simple wiki. Most probably will be using Java on
I have found a XML file through google search that will help me for

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.