String equality also matching numeric 0 with key
| From: | Chris Shenton | Date: | Mon, 09 Feb 2004 19:44:36 +0000 |
| Subject: | String equality also matching numeric 0 with key | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-10935@lists.php.net to get a copy of this message | ||
I'm trying to process results of a PHP LDAP query where one element of
the result is an array, the data is in numeric-indexed elements and
the count of the elements has the index 'count'.
When I try to use Smarty to check for this 'count' key using
{if $var == "count"}
it catches that key name, but ALSO catches the index=0 key.
What am I missing, why's the equality match not working as documented
for "Fred" and "Wilma" at
http://smarty.php.net/manual/en/language.function.if.php
Here's some short test code, template, and output showing this.
Smarty is version 2.6.1.
Thanks.
PHP -----------------------------------------------------------------
<?php
$foo = array('count' => 3,
'0' => 'zero@example.com',
'1' => 'one@example.com',
'2' => 'two@example.com');
echo "<PRE>" . print_r($foo, true) . "</PRE>";
require_once('Smarty.class.php');
$smarty = new Smarty;
$smarty->assign('foo', $foo);
$smarty->display('foreachtest.tpl');
?>
TEMPLATE ------------------------------------------------------------
{foreach item=i key=k from=$foo}
{if $k eq "count"}
k={$k} i={$i}<br>
{/if}
{/foreach}
GENERATED PAGE ------------------------------------------------------
Array
(
[count] => 3
[0] => zero@example.com
[1] => one@example.com
[2] => two@example.com
)
k=count i=3
k=0 i=zero@example.com