-2

With an .ini file lie this:

[sectionName1]
    key1=val1
    key2=val2
[sectionName2]
    key1=val1
    key2=val2
[sectionName3]
    key1=val1
    key2=val2

...how can the section sectionName2 and everything it contains be removed?

Parsing the whole file as text is one way, but since PHP has parse_ini_file, it seems like there would be a built-in function for something like this. The parse_ini_file documentation does not address any special way to alter ini files.

adam rowe
  • 446
  • 1
  • 5
  • 15
  • 2
    We are always glad to help and support new coders but ***you need to help yourself first. :-)*** After [**doing more research**](https://meta.stackoverflow.com/q/261592/1011527) if you have a problem **post what you've tried** with a **clear explanation of what isn't working** and provide [a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Read [How to Ask](http://stackoverflow.com/help/how-to-ask) a good question. Be sure to [take the tour](http://stackoverflow.com/tour) and read [this](https://meta.stackoverflow.com/q/347937/1011527). – Jay Blanchard Aug 21 '18 at 19:07
  • 1
    You can use parse_ini_file that you mentioned to read the file into a multidimensional array with keys corresponding to the section names. Removing a key from an array is easy; you probably already know how to do that. And there are some ideas as to how to write the file back out here: https://stackoverflow.com/questions/5695145/how-to-read-and-write-to-an-ini-file-with-php – Don't Panic Aug 21 '18 at 19:23
  • As far as I know, there is not a built-in function for writing the ini file back out. – Don't Panic Aug 21 '18 at 19:24

1 Answers1

0

parse_ini_file() loads in the ini file specified in filename, and returns the settings in it in an associative array.

(Emphasis by me)

So if you parse the given .ini file you'll have the whole as an array which means you can do:

$ini_array = parse_ini_file("sample.ini");
unset($ini_array['sectionName2']);
fabrik
  • 14,094
  • 8
  • 55
  • 71