0

I need help developing JS code (or PHP if it will be easier and more efficient) that can loop through INI comments, sections, settings and their values.

For example, this is my PHP:

<?php 
$ini_array = parse_ini_file("sample.ini", true);
print_r(json_encode($ini_array));
?>

Here is the JSON that my PHP is outputting:

{"HEADINGONE":{"settingone":"hello","settingtwo":"world"},"HEADINGTWO":{"settingthree":"hi","settingfour":"again"}}

Which is parsing this INI file:

[HEADING ONE]
settingone=hello
settingtwo=world 

[HEADING TWO]
settingthree=hi
settingfour=again

And this is my JS:

$.getJSON('index.php', function (data) {
 $.each(data, function (i,item) {
  alert(i);
 });
});

So far my webpage alerts "HEADING ONE" and "HEADING TWO" but how can I get it to read the rest of the file?

So I can alert "HEADING ONE", "settingone", "hello", "HEADING TWO", and so forth.

Steven Waters
  • 55
  • 1
  • 10
  • Take a look at the output from the PHP page. Posting it here can help too. – Ricardo Souza Feb 02 '13 at 20:37
  • you can inspect the JSON tree in browser console when you inspect request. According to php docs you will have nested array for each section by using `true` in `parse_ini_file`. Add another `each` inside the one you have – charlietfl Feb 02 '13 at 20:49

1 Answers1

1

Try this:

$.getJSON('index.php', function (data) {
 $.each(data, function (i,item) {
   $.each(item, function (settingName, settingValue) {
      alert('setting: ' + settingName + ', value: ' + settingValue);
   });
 });
});
thaJeztah
  • 27,738
  • 9
  • 73
  • 92