Re: [SMARTY] Problems with appending values to an array

From: Date: Thu, 20 Feb 2003 00:12:16 +0000
Subject: Re: [SMARTY] Problems with appending values to an array
References: 1 2 3 4 5 6  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-7963@lists.php.net to get a copy of this message
On Wed, Feb 19, 2003 at 05:10:43PM -0600, Monte Ohrt wrote: > Actually I think this can be done a bit cleaner. > > I backed out the patch, removing the third 'key' parameter. > > You can now merge arrays with append by passing an array as the value. > > example: > > $smarty->assign('foo','bar); > > $smarty->append('foo',array('do','re','mi','fa')); > > If you want to append key/vals: > > $smarty->append('foo',array('do' => 're','mi' > => 'fa')); another bug: array_merge() seems to flatten integer keys, so $smarty->assign('foo',null); $smarty->append('foo',array(3 => 're',4 => 'fa')); will result in array(0=>'re', 1=>'fa) and not in the expected array(3=>'re', 4=>'fa) i'm biased, of course, but i liked my approach with an additional parameter more. have fun messju > I didn't change the append_by_ref, I don't think that is necessary since > it is a reference anyways. > > But give it a try, see if everything works OK. > > Monte > > > On Wed, 2003-02-19 at 11:19, messju mohr wrote: > > On Wed, Feb 19, 2003 at 10:40:23AM -0600, Monte Ohrt wrote: > > > here here! > > > > > > committed. > > > > great :) thanks. > > > > > Monte > > > > > > On Wed, 2003-02-19 at 10:17, messju mohr wrote: > > > > On Wed, Feb 19, 2003 at 05:06:53PM +0100, Joscha Feth wrote: > > > > > > Von: messju mohr [mailto:messju@lammfellpuschen.de] > > > > > > you can only apppend one value at a time, i guess. > > > > > > and you cannot supply your own keys, i'm afraid. > > > > > > > > > > > > maybe we can extend append() to handle this. you have to build your > > > > > > array in php and assign it then to smarty for now. > > > > > > > > > > Thanks for your reply messju, too bad I didn't get a "you are > > > > > such a fool, > > > > > see how easy it is" :-) > > > > > > > > > > > > > > > Greets, > > > > > Joscha > > > > > P.S.: Perhaps if we all shout "here", we get Monte to include it > > > > > in the next > > > > > CVS :-D > > > > > > > > maybe: > > > > > > > > here is what i suggest: give the key as an optional third parameter. > > > > you still have to append your values each (with foreach), otherwise it > > > > wouldn't be compatible (there are people that want to append and array > > > > as a single value! :), but so it is possible to append with arbitrary > > > > keys. > > > > > > > > here is my diff (so i shout "here, me too!" :) > > > > > > > > > > > > Index: Smarty.class.php > > > > =================================================================== > > > > RCS file: /repository/smarty/Smarty.class.php,v > > > > retrieving revision 1.349 > > > > diff -r1.349 Smarty.class.php > > > > 604c604 > > > > < function append($tpl_var, $value = null) > > > > --- > > > > > function append($tpl_var, $value = null, $key=null) > > > > 607,610c607,610 > > > > < foreach ($tpl_var as $key => $val) { > > > > < if ($key != '') { > > > > < if(!@is_array($this->_tpl_vars[$key])) { > > > > < > > > > settype($this->_tpl_vars[$key],'array'); > > > > --- > > > > > foreach ($tpl_var as $name=>$val) { > > > > > if ($var_name != '') { > > > > > if(!@is_array($this->_tpl_vars[$name])) { > > > > > > > > > > settype($this->_tpl_vars[$name],'array'); > > > > 612c612,615 > > > > < $this->_tpl_vars[$key][] = $val; > > > > --- > > > > > if (isset($key)) > > > > > $this->_tpl_vars[$name][$key] = $val; > > > > > else > > > > > $this->_tpl_vars[$name][] = $val; > > > > 620c623,626 > > > > < $this->_tpl_vars[$tpl_var][] = $value; > > > > --- > > > > > if (isset($key)) > > > > > $this->_tpl_vars[$tpl_var][$key] = $value; > > > > > else > > > > > $this->_tpl_vars[$tpl_var][] = $value; > > > > 631c637 > > > > < function append_by_ref($tpl_var, &$value) > > > > --- > > > > > function append_by_ref($tpl_var, &$value, $key=null) > > > > 637c643,646 > > > > < $this->_tpl_vars[$tpl_var][] = &$value; > > > > --- > > > > > if (isset($key)) > > > > > $this->_tpl_vars[$tpl_var][$key] = &$value; > > > > > else > > > > > $this->_tpl_vars[$tpl_var][] = &$value; > > > > > > > > HTH > > > > messju > > > -- > > > Monte Ohrt <monte@ispi.net> > > > > -- > > Webstuhl: Vornehme Ausdrucksweise für die Scheiße, die Webdesignsoftware > > auswirft. > > Holger Veit > -- > Monte Ohrt <monte@ispi.net> > > > -- > Smarty General Mailing List (http://smarty.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php -- Polymorphie: Der Fehler tritt in vielerlei Gestalt auf. Kristian Köhntopp

« previous php.smarty.general (#7963) next »