Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 致命错误:无法使用类型为数组的对象_Php_Arrays_Object - Fatal编程技术网

Php 致命错误:无法使用类型为数组的对象

Php 致命错误:无法使用类型为数组的对象,php,arrays,object,Php,Arrays,Object,所以,当我试图从对象singleton获取数组列表时,出现了这个错误 这是CardType.php上的单例 class CardTypeAPIAccessor extends GuzzleClient { private $client; public function __construct($client) { if($client instanceof GuzzleClient) { $this->

所以,当我试图从对象singleton获取数组列表时,出现了这个错误

这是CardType.php上的单例

class CardTypeAPIAccessor extends GuzzleClient
{

    private $client;

    public function __construct($client) 
    {
        if($client instanceof GuzzleClient)
        {
            $this->client = $client;
        }
        else
        {
            $this->client = parent::getClient();
        }
    }

    public function getCardTypes() {
        $cardTypes = array();

        try 
        {

            $response = $this->client->get('admin/card/type',
                ['headers' => ['Authorization' => $_SESSION['login']['apiKey']]
            ]);

            $statusCode = $response->getStatusCode();
            // Check that the request is successful.
            if ($statusCode == 200) 
            {
                $error = $response->json();
                foreach ($error['types'] as $type) 
                {
                    $cardType = new CardType();
                    $cardType->setCardTypeId($type['card_type_id']);
                    $cardType->setCategory($type['category']);

                    array_push($cardTypes, $cardType);
                }
            }
        }
        catch(RequestException $e) 
        {
            //todo
            echo $e->getRequest() . "</br>";
            if ($e->hasResponse()) 
            {
                echo $e->getResponse() . "</br>";
            }
        }
        return $cardTypes;      
    }
}
class CardTypeApicaccessor扩展客户机
{
私人客户;
公共函数构造($client)
{
if($guzzle客户端的客户端实例)
{
$this->client=$client;
}
其他的
{
$this->client=parent::getClient();
}
}
公共函数getCardTypes(){
$cardTypes=array();
尝试
{
$response=$this->client->get('admin/card/type'),
['headers'=>['Authorization'=>$\u会话['login']['apiKey']]
]);
$statusCode=$response->getStatusCode();
//检查请求是否成功。
如果($statusCode==200)
{
$error=$response->json();
foreach($error['types']作为$type)
{
$cardType=新的cardType();
$cardType->setCardTypeId($type['card\u type\u id']);
$cardType->setCategory($type['category']);
阵列推送($cardTypes,$cardType);
}
}
}
捕获(请求异常$e)
{
//待办事项
echo$e->getRequest()。“
”; 如果($e->hasResponse()) { echo$e->getResponse()。“
”; } } 返回$cardTypes; } }
这是card_type.php上的代码

<?php
     $cardTypes = $cardTypeAPIAccessor->getCardTypes();
     foreach ($cardTypes as $cardType){
     $Type = new CardType();
?>
     <tr>
        <!-- The error is here-->
        <td class="numeric"><?php echo $Type->getCardTypeId($cardType['card_type_id']); ?></td>
        <td class="numeric"><?php echo $Type->getCategory($cardType['category']); ?></td>
     </tr>

错误为:无法将CardType类型的对象用作数组

我的代码出错了吗


谢谢

您似乎已经获得了此行所需的所有值:

$cardTypes = $cardTypeAPIAccessor->getCardTypes();
在我看来,在你的循环中,你只需要:

foreach ($cardTypes as $cardType){
?>
 <tr>
    <td class="numeric"><?php echo $cardType->getCardTypeId(); ?></td>
    <td class="numeric"><?php echo $cardType->getCategory(); ?></td>
 </tr>
foreach($cardTypes作为$cardType){
?>

虽然这实际上只是基于给定代码和缺少的
CardType
类的猜测。但我认为没有必要在循环中生成新对象。

变量
$CardType
是CardType类的一个实例,而不是数组。问题是,当它是对象时,您使用它就像使用数组一样.那么这个

$cardType['card_type_id']
应该是

$cardType->getCardTypeID()

有时,您的响应变得基于对象,无法轻松访问。例如,当您从控制器或任何第三方API中的不同控制器获取数据时

所以你可以用这种方法获取数据

$object->getData(1);   // here 1 is for associated data form.
在我的用例中,当我尝试从不同的控制器访问数据时,我也面临同样的问题

    $attendanceController =  new AttendanceController();

    $offDayData =  $attendanceController->getAttendanceOffDays($req);

    return  $offDayData->getData(1)['response'];
当您尝试从控制器类获取数据时,请以不同的方式包装响应。因此,您必须使用getData(1)方法来访问该响应。在我的解决方案中,我使用了该方法。它在所有情况下都有效,然后您需要从不同的类获取数据
ControllerCals

哪个行号?你能加粗吗?在这个代码上
基本上把
$cardType['card\u type\u id']
改成
$cardType->card\u type\u id
就可以了。如果你想调试,只需在错误行之前使用
var\u dump($cardType);die()