Parse_ini_file converts numeric values to string

Asked
Active3 hr before
Viewed126 times

4 Answers

valuesstringconvertsnumeric
90%

Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,I use the php command parse_ini_file to load the config of an application. Looking at the php documentation there is example, wehere it looks like, that numeric values are excepted, by using those without quotes ". So I used an init-file like that:,Is there a way to process integer values as integer, not as a string, to get the following result:,Making statements based on opinion; back them up with references or personal experience.

Documentation doesn't state anything about returning option values of corresponding type. In fact, they should be strings in common case. (But, if you want, you can check this implementation). Instead you can apply simple callback, for example, via array_walk_recursive():

$array = ["arr" => ["0", "1", "2"], "integer_value" => "3", "float_value" => "-0.5"];
array_walk_recursive($array, function( & $value, $key) {
   if (is_numeric($value)) {
      $value = (string)((int) $value) === $value ?
         (int) $value :
         (double) $value;
   }
});
88%

parse_ini_file() - Parse a configuration file,parse_ini_string — Parse a configuration string, parse_​ini_​file , As of PHP 5.6.1 can also be specified as INI_SCANNER_TYPED. In this mode boolean, null and integer types are preserved when possible. String values "true", "on" and "yes" are converted to true. "false", "off", "no" and "none" are considered false. "null" is converted to null in typed mode. Also, all numeric strings are converted to integer type if it is possible.

load more v
72%

parse_ini_file() loads in the ini file specified in filename, and returns the settings in it in an associative array. ,Example #3 parse_ini_file() parsing a php.ini file, The filename of the ini file being parsed. ,Example #2 parse_ini_file() example

;
This is a sample configuration file
;
Comments start with ';', as in php.ini

[first_section]
one = 1
five = 5
animal = BIRD

[second_section]
path = "/usr/local/bin"
URL = "http://www.example.com/~username"

[third_section]
phpversion[] = "5.0"
phpversion[] = "5.1"
phpversion[] = "5.2"
phpversion[] = "5.3"

urls[svn] = "http://svn.php.net"
urls[git] = "http://git.php.net"
load more v
65%

parse_ini_file() loads in the ini file specified in filename, and returns the settings in it in an associative array. ,Example #3 parse_ini_file() parsing a php.ini file, The filename of the ini file being parsed. ,Example #2 parse_ini_file() example

;
This is a sample configuration file
;
Comments start with ';', as in php.ini

[first_section]
one = 1
five = 5
animal = BIRD

[second_section]
path = "/usr/local/bin"
URL = "http://www.example.com/~username"

[third_section]
phpversion[] = "5.0"
phpversion[] = "5.1"
phpversion[] = "5.2"
phpversion[] = "5.3"

urls[svn] = "http://svn.php.net"
urls[git] = "http://git.php.net"
load more v

Other "values-string" queries related to "Parse_ini_file converts numeric values to string"