【问题标题】:Cannot use object of type stdClass as array in pcategories.php不能在 pcategories.php 中使用 stdClass 类型的对象作为数组
【发布时间】:2016-10-14 06:34:06
【问题描述】:

如何解决这个错误信息?

你能帮忙吗?

致命错误:第 56 行的 C:\Program Files\EasyPHP-Devserver-16.1\eds-www\companygiondaci\application\views\pcategories.php 中无法使用 stdClass 类型的对象作为数组

致命错误:第 56 行的 C:\Program Files\EasyPHP-Devserver-16.1\eds-www\companygiondaci\application\views\pcategories.php 中无法使用 stdClass 类型的对象作为数组

遇到 PHP 错误 严重性:错误 消息:不能使用 stdClass 类型的对象作为数组 文件名:views/pcategories.php

views/pcategories.php

<div class="row-fluid">
                <div class="span12">
                    
                    <button type="button" class="add" onclick="location.href='<?php echo site_url('cpages/addparentctg'); ?>';">ADD PARENT CATEGORIES</button> 
                    
                    <div class="widget-box">
                        <div class="widget-title"><h5>Parent Categories</h5></div>
                        <div class="widget-content">
                        
                        <table border="0" style="width: 100%; height: 90px;">
                            <tr>
                                <td>PARENT NAME</td>
                                <td>DESCRIPTION</td>
                                <td>EDIT</td>
                                <td>DELETE</td>    
                            </td>
                            
                            <?php foreach ($posts as $post): ?>
                            
                            <tr>
                                <td><?php echo $post['ctgparent_name']; ?></td>
                                <td><?php echo $post['ctgparent_description']; ?></td>
                                <td><button type="button" class="edit" onclick="location.href='<?php echo site_url('cpages/editparentctg/'.$post->ctgp_no); ?>';">EDIT</button></td>
                                <td><button type="button" class="delete" onclick="location.href='<?php echo site_url('cpages/editparentctg/'.$post->ctgp_no); ?>';">DELETE</button></td>    
                            </td>    
                                                                                    
                            <?php endforeach; ?>    
                            
                        </table>            
                        </div>
                    </div>                    
                </div>
            </div> 

控制器/Cpages.php

public function pcategories() { 
    
            
        $data['posts'] = $this->Mpages->call_parentctg();

        //$data['ctgparent_name'] = $this->Mpages->call_parentctg();
        //$data['ctgparent_description'] = "Second";//$this->Mpages->retrieve_parentctg();
        
        $this->load->view('pcategories', $data); 
        
    } 

models/Mpages.php

<!-- begin snippet: js hide: false console: true babel: false -->

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    CodeIgniter 将结果行作为对象返回,而不是数组。 所以你需要使用 -> 来访问它。喜欢而不是 $post['ctgparent_name'];使用 $post->ctgparent_name。

    【讨论】:

    • 或者您可以将您的对象转换为数组,例如 $result = json_decode($json, true);
    【解决方案2】:

    如果您尝试使用 $object['property'] 表示法访问对象的属性,则会发生这种情况,而您必须使用 $object-&gt;property。在您的情况下,您正在执行 $post['ctgparent_name']echo $post['ctgparent_description'],您可以将它们更改为分别为$post-&gt;ctgparent_name$post-&gt;ctgparent_description

    如果您使用 codeigniter 活动记录从数据库中获取数据(我怀疑您这样做),还有另一种解决方案。如果你使用$query-&gt;result() 方法,这会给你一个对象。您可以改用$query-&gt;result_array(),这样您现在就有了一系列可以使用$array['whatever'] 表示法的结果。

    此外,如果您使用的是PDOmysqli,则有一些方法可以将数据库结果集作为数组获取。参考官方文档PDO result arrayMysql result arrayMysql associative array

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-17
      • 2012-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多