I'm to pass an array form php to javascript, my solution is use php to generate an html list items like following:
<ul id="data" class="hidden">
<li value="0">
<ul>
<li value="id">1</li>
<li value="productLine">Vintage Cars</li>
</ul>
</li>
<li value="1">
<ul>
<li value="id">2</li>
<li value="productLine">Ships</li>
</ul>
</li>
<li value="2">
<ul>
<li value="id">3</li>
<li value="productLine">Trains</li>
</ul>
</li>
</ul>
and use javascript DOM API to make it to an multidimensional associate array:
function selectProductLine(){
var rootUlNode = document.getElementById('data');
var rootLiNodes = rootUlNode.children;
var hiddenHash = {};
for(var i=0;i< rootLiNodes.length;i++){
var rootLiNodeValue = rootLiNodes[i].getAttribute('value');
var liNodes = rootLiNodes[i].firstElementChild.children;
for(var i=0; i < liNodes.length;i++){
hiddenHash[rootLiNodeValue] = {};
var liNodeValue = liNodes[i].getAttribute('value');
var liNodeContent = liNodes[i].textContent;
hiddengegHash[rootLiNodeValue][liNodeValue] = liNodeContent;
}
}
return hiddenHash;
}
But running the code can crash my browser, so I can't debug it, anyone can point out where my problem is? And I think there should be an element way to transfer data from php to javascript, like using json, could someone provide a better solution or resource on this topic?