php - 迭代数据时,Laravel 无法将 stdClass 类型的对象用作 Job 的数组
问题描述
我正在尝试遍历我的 Laravel 工作文件之一中的一些数据。我成功地获取了我的数据,并且可以在将其记录到文件时看到它,但是由于某些奇怪的原因,当我尝试使用循环迭代我的数据时foreach
,我收到了这个错误:
不能将 stdClass 类型的对象用作数组
public function handle()
{
$filters = json_decode($this->report->discovery_filters);
Log::debug($filters); // gives me my data which I'll attach
foreach ($filters as $key => $findable) {
Log::debug($findable['query']['table']); // errors
die;
}
}
我的数据:
[2021-03-10 14:11:57] local.DEBUG: array (
0 =>
(object) array(
'name' => 'Sessions',
'componentID' => 2435,
'query' =>
(object) array(
'table' => 'data_google_analytics'
// etc... too much data to attach the rest, but 'query' and then 'table' clearly exists
我错过了什么?
解决方案
该错误告诉您问题所在:您正在尝试将对象用作数组。
相反,通过将 true 指定为 json_decode 中的第二个参数,强制将 JSON 解码为数组而不是数组和对象:
$filters = json_decode($this->report->discovery_filters, true);
这将强制 json_decode 将对象转换为关联数组。
推荐阅读
- python - Python对变量中的值求和
- javascript - 在 ReactJS 中发布了一个字符串数组,但在 WebAPI 中得到了一个字符串
- angular - 无法对 Angular 应用程序进行 docker 化
- android - Android MutableLiveData 未反映回主要活动 UI
- python - pandas dayofyear 的日期时间等价物是多少?
- awk - 在列上垂直添加一个字符
- css - 如何控制 Material-UI 垂直标签的宽度?
- azure-ad-b2c - 如何在 Azure B2C 内置注册流程中获取正确的错误消息
- python - 无法通过在 python 中获取用户的输入从字典中的键中删除值
- java - 客户端无法从 Java 中的服务器获取日期