note 114842 added to function.parse-ini-file

From: Date: Sun, 13 Apr 2014 09:59:42 +0000
Subject: note 114842 added to function.parse-ini-file
Groups: php.notes 
Request: Send a blank email to php-notes+get-199258@lists.php.net to get a copy of this message
You may want, in some very special cases, to parse multi-dimensional array with N levels in your ini file. Something like setting[data][config][debug] = true will result in an error (expected "="). Here's a little function to match this, using dots (customizable). <?php function parse_ini_file_multi($file, $process_sections = false, $scanner_mode = INI_SCANNER_NORMAL) { $explode_str = '.'; $escape_char = "'"; // load ini file the normal way $data = parse_ini_file($file, $process_sections, $scanner_mode); if (!$process_sections) { $data = array($data); } foreach ($data as $section_key => $section) { // loop inside the section foreach ($section as $key => $value) { if (strpos($key, $explode_str)) { if (substr($key, 0, 1) !== $escape_char) { // key has a dot. Explode on it, then parse each subkeys // and set value at the right place thanks to references $sub_keys = explode($explode_str, $key); $subs =& $data[$section_key]; foreach ($sub_keys as $sub_key) { if (!isset($subs[$sub_key])) { $subs[$sub_key] = []; } $subs =& $subs[$sub_key]; } // set the value at the right place $subs = $value; // unset the dotted key, we don't need it anymore unset($data[$section_key][$key]); } // we have escaped the key, so we keep dots as they are else { $new_key = trim($key, $escape_char); $data[$section_key][$new_key] = $value; unset($data[$section_key][$key]); } } } } if (!$process_sections) { $data = $data[0]; } return $data; } ?> The following file: <?php /* [normal] foo = bar ; use quotes to keep your key as it is 'foo.with.dots' = true [array] foo[] = 1 foo[] = 2 [dictionary] foo[debug] = false foo[path] = /some/path [multi] foo.data.config.debug = true foo.data.password = 123456 */ ?> will result in: <?php parse_ini_file_multi('file.ini', true); Array ( [normal] => Array ( [foo] => bar [foo.with.dots] => 1 ) [array] => Array ( [foo] => Array ( [0] => 1 [1] => 2 ) ) [dictionary] => Array ( [foo] => Array ( [debug] => [path] => /some/path ) ) [multi] => Array ( [foo] => Array ( [data] => Array ( [config] => Array ( [debug] => 1 ) [password] => 123456 ) ) ) ) ?> ---- Server IP: 194.153.94.10 Probable Submitter: 77.56.108.47 ---- Manual Page -- http://php.net/manual/en/function.parse-ini-file.php Edit -- https://master.php.net/note/edit/114842 Del: integrated -- https://master.php.net/note/delete/114842/integrated Del: useless -- https://master.php.net/note/delete/114842/useless Del: bad code -- https://master.php.net/note/delete/114842/bad+code Del: spam -- https://master.php.net/note/delete/114842/spam Del: non-english -- https://master.php.net/note/delete/114842/non-english Del: in docs -- https://master.php.net/note/delete/114842/in+docs Del: other reasons-- https://master.php.net/note/delete/114842 Reject -- https://master.php.net/note/reject/114842 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#199258) next »