note 108354 added to function.var-export

From: Date: Thu, 19 Apr 2012 09:25:22 +0000
Subject: note 108354 added to function.var-export
Groups: php.notes 
Request: Send a blank email to php-notes+get-187238@lists.php.net to get a copy of this message
Example below shows how to use (e.g.) database stored settings as structurized array/object My input array (pairs settings name and its value) is: Array ( [commision.makler.level.1] => 1.0 [commision.makler.level.2] => 1.2 [commision.makler.level.3] => 1.4 [commision.makler.level.4] => 1.5 [commision.makler.level.5] => 2 [commision.makler.level.6] => 3 [commision.customer.level.0] => 10 [commision.customer.level.1] => 4 [commision.customer.level.2] => 3 [commision.customer.level.3] => 2 [commision.customer.level.4] => 1 ) When i use my paramsToVar() function on this array $oSettings = paramsToVar($myArray,true); ... i'll get struturized object: stdClass Object ( [commision] => stdClass Object ( [makler] => stdClass Object ( [level] => stdClass Object ( [1] => 1.0 [2] => 1.2 [3] => 1.4 [4] => 1.5 [5] => 2 [6] => 3 ) ) [customer] => stdClass Object ( [level] => stdClass Object ( [0] => 10 [1] => 4 [2] => 3 [3] => 2 [4] => 1 ) ) ) ) Now I can easily access my settings or group of settings e.g. $oSettings->makler->level function settingsToVar($inputArray, $returnAsObject =false, $levelSeparator ='.') { $temp = array(); foreach ($inputArray as $key => $setting) { $value = var_export($setting,true); eval('$temp["'.str_replace($levelSeparator,'"]["',$key).'"] = '.$value.';'); } if (!$returnAsObject) { return $temp; } $replacements = array('array ('=>' (object) array('); $varToStr = str_replace(array_keys($replacements), array_values($replacements), var_export($temp,true)); eval('$return = '.$varToStr.';'); return $return; } ---- Server IP: 69.147.83.197 Probable Submitter: 80.53.77.10 ---- Manual Page -- http://www.php.net/manual/en/function.var-export.php Edit -- https://master.php.net/note/edit/108354 Del: integrated -- https://master.php.net/note/delete/108354/integrated Del: useless -- https://master.php.net/note/delete/108354/useless Del: bad code -- https://master.php.net/note/delete/108354/bad+code Del: spam -- https://master.php.net/note/delete/108354/spam Del: non-english -- https://master.php.net/note/delete/108354/non-english Del: in docs -- https://master.php.net/note/delete/108354/in+docs Del: other reasons-- https://master.php.net/note/delete/108354 Reject -- https://master.php.net/note/reject/108354 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#187238) next »