1
$file = dirname(__FILE__) . '/myFile.xml';
$xml = simplexml_load_file("$file");
$json = json_encode($xml);

Here's my json code:

{
    "Commands1": [{
        "Name": "XLS1",
        "Activated": "true",
        "values": [{
            "Name": "Cmd",
            "default": "false"
        }, {
            "Name": "Ls",
            "default": "false"
        }, {
            "Name": "rmdir",
            "default": "false"
        }],
        "Commands2": [{
            "Name": "SKA1",
            "Activated": "true",
            "values": [{
                "Name": "Cp",
                "default": "false"
            }, {
                "Name": "Tcpdump",
                "default": "false"
            }, {
                "Name": "rmdir",
                "default": "false"
            }]

        }]
    }]
}

In my controller I open a XML file, and I'd like to know if it's possible to show its content in a table ? If so can someone give me a simple example ?

The result should be like

Name | Values

xls1 | cmd, ls,rmdir

SKA1 | cp, tcpdump,rmdir

3
  • 1
    Can you please post your Json String ? Commented Mar 8, 2016 at 12:23
  • why you want to have it in json format ? Commented Mar 8, 2016 at 12:46
  • It is already in Json I'd like to show the json in a html table Commented Mar 8, 2016 at 12:50

2 Answers 2

2

The value of the JSON file decode it into a Associative Array and pass it to the view. From there act like you would acct with a normal AssocArray. In controller:

$assoc_array_results = json_decode($json_file_result);

return $this->render('view.html.twig', array('results' => $assoc_array_results));
Sign up to request clarification or add additional context in comments.

3 Comments

Thank u for your answer, but that's exactly my question, I can't get the array in my twig view but i don't know how to show it in a table format instead of string... i'm a beginner
in twig: {% for result in results %} <td>{{ result['Name'] }}</td> <td> {{ result['Activated'] }} </td> // And so on {% endfor %} Just loop throw the array
the response of the controller
1

Well, it certainly is possible. You need to first decode it into an array.

$s = json_decode($json,true);

Then, comes the foreach() loop. You will have to consider each and every key-value pair and then iterate that loop to print your result.
PS: Apparently, I found a mistake in your Json String. Your Commands2 is becoming a subset of Commands1. Here is your correct Json.

{
"Commands1": [
{
  "Name": "XLS1",
  "Activated": "true",
  "values": [
    {
      "Name": "Cmd",
      "default": "false"
    },
    {
      "Name": "Ls",
      "default": "false"
    },
    {
      "Name": "rmdir",
      "default": "false"
    }
  ]
}
],
"Commands2": [
{
  "Name": "SKA1",
  "Activated": "true",
  "values": [
    {
      "Name": "Cp",
      "default": "false"
    },
    {
      "Name": "Tcpdump",
      "default": "false"
    },
    {
      "Name": "rmdir",
      "default": "false"
    }
  ]
  }
  ]
}

And, Here is a reference, You can check How will your table look like when from JSON.http://json2table.com/
Now, Regarding your table, No one is gonna write the code for you on stack. So, I suggest go Baby steps if you wanna learn it. Here is a reference for you http://www.w3schools.com/php/php_looping_for.asp

Comments

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.