note 80616 added to ref.array

From: Date: Fri, 25 Jan 2008 10:00:09 +0000
Subject: note 80616 added to ref.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-135396@lists.php.net to get a copy of this message
A usefull function that returns a flat array. I use it in a template system. Let the user pass a multidimensional array. Convert it using my function. Then use <?php $array = flatten($array,'','{$','}','->'); echo str_replace(array_keys($array),array_values($array),$template) /** * Flattens out an multidimension array * Using the last parameters you can define the new key based on the old path. * @param array $array A multidimension array * @param string $prefix Internal perfix parameter - leave empty. * @param string $start_string What string should start the final array key? * @param string $end_string What string should end the final array key? * @param string $seperator The string that should seperate the piecies in final array key path * @return array Returns the flat array */ function flatten($array, $start_string= '{$',$end_string= '}',$seperator='->',$prefix="") { $return = array(); foreach($array as $key=>$value) { if (is_array($value)) { $return = array_merge($return, Parser_method_replace::flatten($value, $prefix.$key.$seperator,$start_string,$end_string,$seperator)); } else $return [$start_string.$prefix.$key.$end_string] = $value; } return $return; } } ?> Example: $template = 'My string with replacement {$test->subkey}'; {$test->subkey} will get replaced with $array['test']['subkey'] ---- Server IP: 195.184.96.15 Probable Submitter: 83.91.197.162 ---- Manual Page -- http://www.php.net/manual/en/ref.array.php Edit -- https://master.php.net/note/edit/80616 Del: integrated -- https://master.php.net/note/delete/80616/integrated Del: useless -- https://master.php.net/note/delete/80616/useless Del: bad code -- https://master.php.net/note/delete/80616/bad+code Del: spam -- https://master.php.net/note/delete/80616/spam Del: non-english -- https://master.php.net/note/delete/80616/non-english Del: in docs -- https://master.php.net/note/delete/80616/in+docs Del: other reasons-- https://master.php.net/note/delete/80616 Reject -- https://master.php.net/note/reject/80616 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#135396) next »