0

I have the following JSON Data:

{
  "tables": [
    {
      "name": "PrimaryResult",
      "columns": [
        {
          "name": "TimeGenerated",
          "type": "datetime"
        },
        {
          "name": "Computer",
          "type": "string"
        }
      ],
      "rows": [
        [
          "2022-12-03T21:58:48.519866Z",
          "DESKTOP-KAFCPRF"
        ],
        [
          "2022-12-03T21:58:48.5198773Z",
          "DESKTOP-KAFCPRF"
        ]
      ]
    }
  ]
}

I'm using this to and trying to find a way to parse the columns as tables headers(TH) and the row data as(TR).

This is what I have:

  $jsonObjs = json_decode($data, true);
  echo "<pre>" . var_dump($jsonObjs) . "</pre>";
  foreach($jsonObjs as $a){
      foreach($a[0] as $b) {
        foreach($b[1] as $key => $value){
          echo $key . " : " . $value . "<br />";
        }
      }
  }

but my results are coming up like:

name : Computer
type : string
0 : 2022-12-03T21:58:48.5198773Z
1 : DESKTOP-KAFCPRF
1
  • 1
    You need to run through this twice. First, extract the "columns", so you know how to label the header row of your table. Then, just run through the "rows" list to do your printing. Commented Dec 4, 2022 at 1:06

1 Answer 1

1

Here is a loop that will go through the columns and rows. From here it's fairly simple to adjust the logic for building a table. If you need any further help, let me know.

foreach($jsonObjs["tables"] as $a)
  {
    // Columns
    foreach($a["columns"] as $key => $value)
    {
        $cName = $value["name"];
        $cType = $value["type"];
        
        echo("Column name is: ".$cName);
        echo("Column type is: ".$cType);
    }
    
    // Rows:
    foreach($a["rows"] as $key => $value)
    {
        $rValue1 = $value[0];
        $rValue2 = $value[1];
        
        echo("Row: " . $rValue1 . " | " . $rValue2);
    }
  }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. This helped me a lot. Much cleaner then the solution I originally discovered! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.