Php 在数组中迭代对象的问题

Php 在数组中迭代对象的问题,php,mysql,Php,Mysql,我有下面这个方法,它负责给我带来一个对象数组(blog) 我知道数组不是空的。我要做的是以这种方式迭代数组中的对象: <?php foreach ($blog_category as $item): ?> <h2> <a href="<?php echo Utils::createLink('detail-post', array('id' => $item->getId())); ?>"><?php ec

我有下面这个方法,它负责给我带来一个对象数组(blog)

我知道数组不是空的。我要做的是以这种方式迭代数组中的对象:

<?php foreach ($blog_category as $item): ?>
    <h2>
        <a href="<?php echo Utils::createLink('detail-post', array('id' => $item->getId())); ?>"><?php echo $item->getTitle(); ?></a>
    </h2>
    <p class="lead">
        by <a href="<?php echo Utils::createLink('detail-post', array('id' => $item->getId())); ?>"><?php echo $item->getAuthor(); ?></a>
    </p>
    <p><i class="fa fa-clock-o"></i> Postado em 
        <?php $date = strtotime($item->getDate());
            $br_format = date("d/m/Y g:i A", $date);
            echo $br_format;
        ?></p>
    <hr>
    <a href="<?php echo Utils::createLink('detail-post', array('id' => $item->getId())); ?>">                
        <img class="img-responsive img-hover" src="../web/img/1850_james_ancestry.JPG" alt=""/>
    </a>
    <hr>
    <p><?php echo $item->getPre(); ?></p>
    <a class="btn btn-primary" href="<?php echo Utils::createLink('detail-post', array('id' => $item->getId())); ?>">Leia mais <i class="fa fa-angle-right"></i></a>

    <hr>
<?php endforeach; ?>  

通过

邮递员




但是页面没有呈现任何内容


任何光线都可以欣赏。

所使用的对象都是从strClass()创建的,因此没有任何与之关联的get方法

因此,像这样更改脚本,以便直接访问这些对象的属性,而不是期望它们具有关联的
->getxxx()
方法

<a href="<?php echo Utils::createLink('detail-post', array('id' => $item->id)); ?>">
<?php echo $item->title; ?>
</a>


谢谢@RiggsFolly对你的帮助。我解决了我的问题,做了一些修改作为休耕:

public function getListaBlogsById($id) {
    try {
        $result = array();
        $blog = new Blog();
        $statement = $this->getDb()->prepare('SELECT * FROM Blog WHERE categoria_id = :id');
        $statement->bindParam(':id', $id);
        if ($statement->execute()) {
            while ($row = $statement->fetch()) {
                BlogMapper::map($blog, $row);
                $result[$blog->getId()] = $blog;
            }
        }         

    } catch (Exception $ex) {
        $ex->getTrace();
    }

    return $result;
}


当你什么都不说的时候,你是什么意思?白色屏幕?错误信息?没有错误,只是一张白页。你好,谢谢你的帮助。但是当我放置$result=$statement->fetchAll(PDO::FETCH_类)时;我将有一个对象数组,它的属性像get?这就是你说的你想要的。如果您想要一个数组数组,那么使用
$item->getId()
就更没有意义了!查看
$blog\u category
的实际填充位置可能会很有用,因为目前我只是假设根据您提供的代码,它包含
getListaBlogsById($id)
的结果,是的,$result=$statement->fetchAll(PDO::FETCH\u类)我仍然得到白色页面。我无法通过get方法访问
if(array_key_exists('id',$_GET)){$blog_category=$dao->getListaBlogsById($_GET['id');}否则{Utils::redirect('blog');}
$result = $statement->fetchAll();
$result = $statement->fetchAll(PDO::FETCH_CLASS);
public function getListaBlogsById($id) {
    try {
        $result = array();
        $blog = new Blog();
        $statement = $this->getDb()->prepare('SELECT * FROM Blog WHERE categoria_id = :id');
        $statement->bindParam(':id', $id);
        if ($statement->execute()) {
            while ($row = $statement->fetch()) {
                BlogMapper::map($blog, $row);
                $result[$blog->getId()] = $blog;
            }
        }         

    } catch (Exception $ex) {
        $ex->getTrace();
    }

    return $result;
}
public static function map(Blog $blog, array $properties) {

    if (array_key_exists('id', $properties)) {
        $blog->setId($properties['id']);
    }

    if (array_key_exists('blog_author', $properties)) {
        $blog->setAuthor($properties['blog_author']);
    }

    if (array_key_exists('blog_article_title', $properties)) {
        $blog->setTitle($properties['blog_article_title']);
    }

    if (array_key_exists('blog_date_creation', $properties)) {
        $blog->setDate($properties['blog_date_creation']);
    }

    if (array_key_exists('blog_article_pre', $properties)) {
        $blog->setPre($properties['blog_article_pre']);
    }

    if (array_key_exists('blog_article', $properties)) {
        $blog->setArticle($properties['blog_article']);
    }

    if (array_key_exists('categoria_id', $properties)) {
        $blog->setCategoria_id($properties['categoria_id']);
    }
}