0

I try to read the file names.ini below into a php array and then access certain repeating sections within the code.

My code does not work correct, the base problem seems to be, that ini files don't have a structure to nest sections.

But: could there be a workaround if the code would be slightly modified???

File names.ini:

[baseSetup]

  sections[] = name
  sections[] = place

[name][1]
  DummyName1
[name][2]
  DummyName2
[name][3]
  DummyName3

[place][]
New York
[place][]
Texas
[place][]
San Francisco

Php-script to read the file:

<?php
   $setup =  parse_ini_file("names.ini",true);
   print_r($setup);
   echo("<br/> ---------------<br/>");
   
   echo ("setup[name = " . $setup["name"] . "<br/>");
   print_r($setup["name"]);
   echo ("<br/><br/>");

   foreach ($setup["baseSetup"] as $key => $val) {
     echo ("key: $key:<br/>");
     print_r($val);
     
     foreach($val as $idx) {
     
       echo ("<br/>count of $idx: " . count($setup[$idx]) . "<br/>");
     
     }     
     echo ("<br/>");
     
   }
    
?>

Further specification:

I could write it this way:

[baseSetup]
  nSections = 2
  
[sections] 
  name = 3
  place = 4
  
[name1] 
content = DummyName1
 
[name2]  
content = DummyName2

[name3]  
content = DummyName3      

[place1]  
content = New York
[place2]  
content = Texas
[place3]  
content= San Francisco
[place4]  
content= Boston

And read it like this:

 <?php
   $setup =  parse_ini_file("names2.ini",true);
   
   print_r($setup);
   echo("<br/> ---------------<br/>");
   
   $nNames = $setup["sections"]["name"];
   $nPlaces = $setup["sections"]["place"];
   
   for ($i=1;$i<=$nNames;$i++) {
     $key = "name".$i;
     echo ("name = "); print_r($setup[$key]); echo("<br/>");
   }

   for ($i=1;$i<=$nPlaces;$i++) {
     $key = "place".$i;       
     echo ("place = "); print_r($setup[$key]); echo("<br/>");
   }
    

?>

So this would work as a basis. But it would like to access the names and places without having to explicitly name their number in the ini file.

5
  • What do you expect from your code & data sample? What is the error you are actually facing? What is your debug trace? Commented Feb 21, 2024 at 14:42
  • You have very weird ini file format. It should consist of sections and key/value pairs Commented Feb 21, 2024 at 14:45
  • 1
    I did edit the question. Maybe this is helpful. Thank you. Commented Feb 21, 2024 at 15:08
  • Furthermore: I would like to add sections insetad of strings; e.g. instead of "New York" I would like to have [City] name = New York citizens = 8,335,897 Commented Feb 21, 2024 at 15:20
  • There is a related question for this topic: stackoverflow.com/questions/14614866/nested-arrays-in-ini-file I started with this configuration. But I could not manage to access the array elements individually (in the question there also is only a print_r to show that the nesting of sections is possible, but not an example for access the elements). Commented Feb 21, 2024 at 16:14

0

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.