0

With PHP, how can I make a table that uses generic references to key values instead of specific keys? I use the table, at the bottom of question, code with the array below. However, I want to use the same table code for any array (different keys and different number of keys). Any suggestions will be greatly appreciated.

Example array that is hardcoded into the PHP table code:

{
    "port": "Et2",
    "name": "**Description**",
    "status": "connected",
    "vlan": "in",
    "duplex": "Po301",
    "speed": "full",
    "type": "10G    10GBASE-CR        "
},

I would like the table code to work for any generic array such as the one below. Also not a value could be another array. I the case a value is an array, I would like all the contents of the array in one table cell even if it needs to wrap to the next line. There could be 100's of values in the array.

{
    "key1": "value 1",
    "key2": "value 2",
    "key3": "value 3",
    "key4": ["value 1a","value 2a","value 3a"]
},

I am currently using the following code to take the elements of the specific array and put it into a table but want to use table code that works for output for many network commands.

for($i = 0; $i < sizeof($data); $i++)
{
    $temp .= "<tr>";
    $temp .= "<td>" . $data[$i]["port"] . "</td>";
    $temp .= "<td>" . $data[$i]["name"] . "</td>";
    $temp .= "<td>" . $data[$i]["status"] . "</td>";
    $temp .= "<td>" . $data[$i]["vlan"] . "</td>";
    $temp .= "<td>" . $data[$i]["duplex"] . "</td>";
    $temp .= "<td>" . $data[$i]["speed"] . "</td>";
    $temp .= "<td>" . $data[$i]["type"] . "</td>";
    $temp .= "</tr>";
}

/*End tag of table*/
$temp .= "</table>";

/*Printing temp variable which holds table*/
echo $temp;
gordo8888
  • 1
  • 2
  • possible [duplicate](https://stackoverflow.com/questions/4746079/how-to-create-a-html-table-from-a-php-array) – jibsteroos Sep 11 '20 at 20:20
  • There's `foreach` that will tell you both the key and value. I guess from that point you can figure the rest out. – SparK Sep 11 '20 at 23:49

2 Answers2

0

Process the array of arrays like this

$temp .= "<table>";
foreach ($data as $subArray){
    $temp .= "<tr>";
    foreach ($subArray as $key => $val) {
        $temp .= "<td>" . $val . "</td>";  
    }
    $temp .= "</tr>";
}
$temp .= "</table>";
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • This works perfectly. Thanks! Only one issue that I missed in my question. Occasionally the value can be an array. Like this: { "vlan_id": "1", "name": "default", "status": "active", "interfaces": [ "Gi4/0/11", "Te5/0/1" ] } – gordo8888 Sep 11 '20 at 20:32
  • `$temp .= "" . (is_array($val) ? implode(', ', $val) : $val) . "";` should output the value as a CSV in that case (this uses a [ternary operator](https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary) but you could do it with a standard `if` conditional instead). Edit: ah, though I see the array value may have additional arrays (i.e. `interfaces`). Your best bet is to write a separate conditional inside the inner `foreach` to check `if (is_array($val)) {` and then write your custom logic to handle these cases. – WOUNDEDStevenJones Sep 11 '20 at 20:45
  • Based on what you said. I am going to try to check if it is an array and implode it. Then hopefully the whole string will print into a table cell. – gordo8888 Sep 11 '20 at 22:02
0

I took what @RiggsFolly and @WOUNDEDStevenJones said and combined them. Inside the second foreach loop, I check if the $val is an array. If it is I implode it into a string. Thank You.

$temp .= "<table>";
foreach ($data as $subArray){
    $temp .= "<tr>";
    foreach ($subArray as $key => $val) {
        if (is_array($val)) { #new if
                $val = implode(", ",$val); #new if
                } #new if
        $temp .= "<td>" . $val . "</td>";
    }
    $temp .= "</tr>";
}
$temp .= "</table>";
gordo8888
  • 1
  • 2