duanfei1268 2014-11-23 01:48
浏览 30
已采纳

具有Smarty的二维数组 - 致命错误不能使用类型的对象

I am working on a PHP project and I am have multidimensional array and I am trying to loop through the array in Smarty to display it.

I am creating the following code in PHP

if (count($routeFields) > 1)
                {
                    $savedRoutes[$count] = new RouteDetails();

                    $savedRoutes[$count]->destination = $routeFields[DESTINATION];
                    $savedRoutes[$count]->gateway = $routeFields[GATEWAY];
                    $savedRoutes[$count]->genmask = $routeFields[GENMASK];
                    $savedRoutes[$count]->flags = $routeFields[FLAGS];
                    $savedRoutes[$count]->metric = $routeFields[METRIC];
                    $savedRoutes[$count]->ref = $routeFields[REF];
                    $savedRoutes[$count]->use = $routeFields[USEF];
                    $savedRoutes[$count]->iface = $routeFields[IFACE];

                    $count++;
                }
return $savedRoutes;

class RouteDetails
    {
        public $destination;
        public $gateway;
        public $genmask;
        public $flags;
        public $metric;
        public $ref;
        public $use;
        public $iface;
    }

Below is how I am calling my function and giving it to Smarty

$smarty = new Smarty();
                    $smarty->setTemplateDir("templates");

                    $routeManagement = new RouteManagement();
                    $result = $routeManagement->getRoutes();

                    $smarty->assign("routes", $result);

                    $smarty->display('routes.tpl');

Below is my Smarty template

<table>
    <tr>
        <td>Destination</td>
        <td>Gateway</td>
        <td>Genmask</td>
        <td>Flags</td>
        <td>Metric</td>
        <td>Ref</td>
        <td>Use</td>
        <td>Iface</td>
    </tr>
    {foreach from=$routes key=key item=item}
        <tr>
            <td>{$item.destination}</td>
        </tr>
    {/foreach}
</table>

I get the following error displayed:

Fatal error: Cannot use object of type RouteDetails as array in /var/www/html/RouteManagement/templates_c/06dfeb8eb18eac12fde3a6f643d7f25678e14aaf.file.routes.tpl.php on line 46

Below is an output of $result

Array
(
    [0] => RouteDetails Object
        (
            [destination] => 192.168.1.0
            [gateway] => *
            [genmask] => 255.255.255.0
            [flags] => U
            [metric] => 0
            [ref] => 0
            [use] => 0
            [iface] => eth0
        )
)
  • 写回答

1条回答 默认 最新

  • duanbi2760 2014-11-23 02:27
    关注

    Ah, I get it. $item is an object, not an array. the smarty $item.destination means you are trying to access it like $item['destination']. Try $item->destination

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料