访问没有按预期工作的accossiated数组值

时间:2014-07-16 05:40:29

标签: php

我有这个函数返回一个数组对象

static public function GetFeaturedArticle($db) {

    try {

        $statement = $db->prepare("SELECT a.*, u.username FROM articles a LEFT JOIN users u ON a.user_id = u.id WHERE a.featured = TRUE LIMIT 1");
        $statement->execute();      

        while ($record = $statement->fetch(PDO::FETCH_ASSOC)) {
            // i have to process some data here and i want to return 
            // associative array back
            $obj = new self($db);
            $obj->processInput($record);
            $obj->processPhotoField();

        }

    } catch (PDOException $ex) {
        if (HOST_TYPE ==="dev") {
            echo "<pre>"; print_r($ex); echo "</pre>";
        }
        throw ($ex);
    }
    // the return type i'm not sure of
    return get_object_vars($obj);       
}

我试图访问我的属性

<?php echo $this->featured_article->data["id"]; ?>

但是我得到一个致命的错误: 注意:尝试在第27行的C:\ wamp \ www \ winepress \ www \ templates \ home.tpl.php中获取非对象的属性

这是我的var转储

array (size=8)
  'db' => 
    object(PDO)[1]
  'id' => int 2
  'data' => 
    array (size=11)
      'id' => int 2
      'category_id' => int 1
      'user_id' => int 6
      'username' => string 'kirk' (length=4)
      'title' => string 'In ultricies, tortor in placerat dignissim, sapien leo feugiat augue' (length=68)
      'description' => string 'In suscipit orci ac turpis ultricies tincidunt. Quisque volutpat turpis ut leo interdum sodales. Sed fermentum magna consequat ante ultrices, a egestas est posuere. Phasellus varius massa in est sollicitudin laoreet' (length=215)
      'content' => string '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In suscipit orci ac turpis ultricies tincidunt. Quisque volutpat turpis ut leo interdum sodales. Sed fermentum magna consequat ante ultrices, a egestas est posuere. Phasellus varius massa in est sollicitudin laoreet. Maecenas sit amet consequat orci. Aliquam pharetra ante sit amet urna volutpat feugiat. Praesent sed sapien in est pellentesque mollis. Proin nisl felis, imperdiet quis nunc at, bibendum sagittis elit. Vestibulum in arcu quis est phare'... (length=3214)
      'created_date' => string '2014-07-10 14:05:55' (length=19)
      'featured' => int 1
      'enable_comments' => int 0
      'photo' => 
        object(Image)[6]
          public 'verbose_debug' => boolean false
          public 'error' => string '' (length=0)
          public 'errno' => int 0
          public 'filename' => string 'images/articles/pic_2.jpg' (length=25)
          public 'mimetype' => string 'image/jpeg' (length=10)
          public 'width' => int 484
          public 'height' => int 266
          public 'htmlDimensions' => string 'width="484" height="266"' (length=24)
          public 'getimagesize_data' => 
            array (size=7)
              ...
          public 'getimagesize_info' => 
            array (size=0)
              ...
          protected 'files_superglobal_entry' => null
          public 'thumbnailJpegQuality' => int 85
  'allowedFields' => 
    array (size=11)
      0 => string 'id' (length=2)
      1 => string 'category_id' (length=11)
      2 => string 'user_id' (length=7)
      3 => string 'username' (length=8)
      4 => string 'title' (length=5)
      5 => string 'description' (length=11)
      6 => string 'content' (length=7)
      7 => string 'created_date' (length=12)
      8 => string 'featured' (length=8)
      9 => string 'enable_comments' (length=15)
      10 => string 'photo' (length=5)
  'validation' => 
    array (size=4)
      'title' => 
        array (size=2)
          0 => string 'Validation::checkRequired' (length=25)
          1 => string 'Validation::checkName' (length=21)
      'description' => 
        array (size=1)
          0 => string 'Validation::checkRequired' (length=25)
      'content' => 
        array (size=1)
          0 => string 'Validation::checkRequired' (length=25)
      'photo' => 
        array (size=1)
          0 => string 'ArticleModel::checkPhotoIsImageObject' (length=37)
  'input' => 
    array (size=11)
      'id' => int 2
      'category_id' => int 1
      'user_id' => int 6
      'photo' => string 'pic_2.jpg' (length=9)
      'title' => string 'In ultricies, tortor in placerat dignissim, sapien leo feugiat augue' (length=68)
      'description' => string 'In suscipit orci ac turpis ultricies tincidunt. Quisque volutpat turpis ut leo interdum sodales. Sed fermentum magna consequat ante ultrices, a egestas est posuere. Phasellus varius massa in est sollicitudin laoreet' (length=215)
      'content' => string '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In suscipit orci ac turpis ultricies tincidunt. Quisque volutpat turpis ut leo interdum sodales. Sed fermentum magna consequat ante ultrices, a egestas est posuere. Phasellus varius massa in est sollicitudin laoreet. Maecenas sit amet consequat orci. Aliquam pharetra ante sit amet urna volutpat feugiat. Praesent sed sapien in est pellentesque mollis. Proin nisl felis, imperdiet quis nunc at, bibendum sagittis elit. Vestibulum in arcu quis est phare'... (length=3214)
      'created_date' => string '2014-07-10 14:05:55' (length=19)
      'featured' => int 1
      'enable_comments' => int 0
      'username' => string 'kirk' (length=4)
  'debugOutput' => null
  'errors' => 
    array (size=11)
      'id' => string '' (length=0)
      'category_id' => string '' (length=0)
      'user_id' => string '' (length=0)
      'username' => string '' (length=0)
      'title' => string '' (length=0)
      'description' => string '' (length=0)
      'content' => string '' (length=0)
      'created_date' => string '' (length=0)
      'featured' => string '' (length=0)
      'enable_comments' => string '' (length=0)
      'photo' => string '' (length=0)

如何访问我的数据属性并回显我的数据

<?php echo $this->featured_article->data["id"]; ?>

1 个答案:

答案 0 :(得分:1)

您尝试使用

访问类属性
featured_article->data

但根据您的var_dump输出,您有一个数组。

所以你必须用

访问它
featured_article['data']

所以你的行应该是

<?php echo $this->featured_article['data']['id']; ?>