Re: [SMARTY] Problems with appending values to an array
| From: | messju mohr | Date: | Thu, 20 Feb 2003 21:12:44 +0000 |
| Subject: | Re: [SMARTY] Problems with appending values to an array | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.smarty.dev php.smarty.general |
| Request: | Send a blank email to php-smarty-dev+get-783@lists.php.net to get a copy of this message | ||
On Thu, Feb 20, 2003 at 11:04:13AM -0600, Monte Ohrt wrote:
> OK, I think I've got this append() problem figured out (and committed.)
yes, i saw this. that is a great integration of the suggestions of
joscha. thank you.
> There are two issues here, one is merging vs. appending, and the other
> is applying the merge/append to a key value not in the appended array.
>
> For the merge issue, I have adjusted append() and append_by_ref() with a
> third value of $merge:
>
> function append($tpl_var, $value=null, $merge=false) {}
>
> function append_by_ref($tpl_var, &$value, $merge=false) {}
>
> If you supply the third paramter true (and you pass and array as param
> 2), then the appened values will be merged instead of appended. This
> also fixes the problem of array_merge() flattening the keys, this won't
> happen. But, you must be careful to only use merge if you are merging
> arrays with unique keys! If you merge numbered arrays (or any two arrays
> with similar key values), they will write over top of each other since
> keys are preserved. That is probably why array_merge() behaves that way.
>
> Examples:
>
> $smarty->assign('foo','bar');
> $smarty->append('foo',array(3 => 're', 4 => 'fa'));
>
> result:
>
> Array
> (
> [0] => bar
> [1] => Array
> (
> [3] => re
> [4] => fa
> )
>
> )
>
> $smarty->assign('foo','bar');
> $smarty->append('foo',array(3 => 're', 4 => 'fa'),
> true);
>
> result:
>
> Array
> (
> [0] => bar
> [3] => re
> [4] => fa
> )
>
>
> Watch out for this snafu!!
>
> $smarty->assign('foo',array('do','re','mi'));
> $smarty->append('foo',array('fa','sol','la'), true);
>
> result:
>
> Array
> (
> [0] => fa
> [1] => sol
> [2] => la
> )
>
oh, good catch. i would have overlooked that.
> The first three got overwritten because both arrays have keys 0,1,2.
> Is this how we want it to work? Or just let it work like array_merge()
> does? btw, section loops will NOT work right with arrays that don't have
> sequential numbered keys, such as 0,3,4.
>
> Now as for assigning to a given key not part of the value being
> appended, I'm considering that this should not be a function of
> append(). You can do this:
>
> $smarty->append('foo',array('mykey' => $array), true);
>
> Or in the case of append_by_ref:
>
> $array = array('key' => $array);
> $smarty->append('foo', $array, true);
>
> If we get a strong enough vote, we can put a fourth 'key' parameter on,
> but this just seems a bit overkill for the function, and doesn't fit
> well with the params.
i think append() is complex and powerful enough as it is. so i
wouldn't vote for a fourth parameter.
regards
messju
> Monte
>
> On Wed, 2003-02-19 at 18:12, messju mohr wrote:
> > 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
> --
> Monte Ohrt <monte@ispi.net>
>
>
> --
> Smarty General Mailing List (http://smarty.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
--
Kommentierter Quellkode: Hier programmiert ein Germanist oder Anglist.
Manfred Worm Schäfer