Reply to Re: [smarty] addressing associative array

Your name:

Reply:


Posted by Michael Placentra II on 04/25/07 22:15

Man-wai Chang wrote:
> Given this assoc array:
>
> $dtl_array[0]['uom_array'][0][0]='key1';
> $dtl_array[0]['uom_array'][0][1]='value1';
> $dtl_array[0]['uom_array'][1][0]='key2';
> $dtl_array[0]['uom_array'][1][1]='value2';
> $dtl_array[1]['uom_array'][0][0]='key3';
> $dtl_array[1]['uom_array'][0][1]='value3';
> $dtl_array[1]['uom_array'][1][0]='key4';
> $dtl_array[1]['uom_array'][1][1]='value4';
>
>
> $sample->assign("dtl_array",$dtl_array);
> $sample->display();
>
> How could I create a combobox out of $dtl_array in the templates?
> The following does not work:
>
> <select name="aname">
> {section name=mm start=0 loop=$dtl_array.0.uom_array step=1}
> <option value="{$dtl_array.0.uom_array.mm.0}">
> {$dtl_array.0.uom_array.mm.1}
> {/section}
> </select>
>

Why do you have such a strange array? It's not as easy to iterate through its values if it's counted in base two, you should alter $dtl_array through PHP into something easier to deal with first:


<?php
$dtl_array = generate_bizarre_array();

$cool_array = array();

$SoFdtl_array = count( $dtl_array );
for( $i = 0; $i < $SoFdtl_array; ++$i )
{
$SoFdtl_array_I_uom_array = count( $dtl_array[$i]['uom_array'] );
for( $j = 0; $j < $SoFdtl_array_I_uom_array; ++$j )
$cool_array[] = array( 'key' => $dtl_array[$i]['uom_array'][$j][0],
'value' => $dtl_array[$i]['uom_array'][$j][1] );
}

$sample->assign( 'coolness', $cool_array );
$sample->display( 'it.tpl' );
?>

(it.tpl:)
<select name="JohnDoe">
{foreach from=$coolness item="opt"}
<option value="{$opt.key}">{$opt.value}</option>
{/foreach}
</select>



This assumes that however you get that binary-indexed array, it comes from a dynamic source. Otherwise, the PHP code would be much simpler. {foreach} is also easier to use (IMO). I suggest you also throw in a {foreachelse} if this really is a dynamic source.

-Mike PII

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация