显示对象数组的值


Display values of array of objects

如何显示存储在会话中的对象数组的值

我这样做

//从数据库中获取国家数组

$countries = array();
    while($row = mysql_fetch_array($result)){
        $country = new Country();
        $country->setCountryName($row['country_name']);
        $country->setCountryNo($row['country_no']);
        $country->setCountryZipCode($row['country_zipcode']);
        $countries[]=$country;
    }
$_SESSION['countries']=$countries;

则显示会话的值,如下所示

<select name="countries" id="countries">
        <?php foreach ($_SESSION['countries'] as $i=>$country ){?>
        <option><?php echo $_SESSION['countries'][$i]=>$country.getCountryName()?></option>   //here's the error
        <?php
        }
    ?>
    </select>

显示session值时出现错误,请提供帮助?

试试这个:

<?php echo $country->getCountryName(); ?>

不能在会话中保存对象。我很抱歉。但是您可以将它们存储为属性数组,并在每次脚本启动时恢复它。