I am iterating over an array to display some data, and at the same time creating another array which will be used to create JSON and then assign it to a JavaScript variable.
<?php
$otherStuff=array();
foreach($myArray AS $row)
{
echo("<tr data-id='{$row['id']}'><td>{$row['firstname']}</td><td>{$row['firstname']}</td></tr>");
$otherStuff[$row['id']]=$row['otherStuff'];
}
echo('<script type="text/javascript">var otherStuff='.json_encode($otherStuff).';</script>');
?>
This just doesn't seem like a very clean way to do this task. Instead I am wondering if it would be better to create some hidden HTML, and then clientside parse it to create the desired JavaScript variable.
Is doing so possible? Is it a good idea, or should I do something else? If possible, how?
Thank you