php - 如何访问从excel文件生成的复杂数组
问题描述
我从我的 excel 文件中生成了数组,我想访问这个数组,但是我收到了这个错误:Property [name] does not exist on this collection instance。
我已经能够从我的 excel 文件生成数组,但是我很难访问它。数组的dd($data)
输出如下所示。
SheetCollection {#394 ▼
#title: ""
#items: array:3 [▼
0 => RowCollection {#590 ▼
#heading: array:3 [▼
0 => "name"
1 => "email"
2 => "phone"
]
#title: "Sheet1"
#items: array:3 [▼
0 => CellCollection {#620 ▼
#title: null
#items: array:3 [▼
"name" => "emma"
"email" => "emma@yahoo.com"
"phone" => 89889898.0
]
}
1 => CellCollection {#413 ▼
#title: null
#items: array:3 [▼
"name" => "Godstime John"
"email" => "jgodstime10@yahoo.com"
"phone" => 909989898.0
]
}
2 => CellCollection {#571 ▼
#title: null
#items: array:3 [▼
"name" => "John Emma"
"email" => "jgh@email.com"
"phone" => 9090898.0
]
}
]
}
1 => RowCollection {#595 ▼
#heading: array:1 [▼
0 => ""
]
#title: "Sheet2"
#items: []
}
2 => RowCollection {#418 ▼
#heading: array:1 [▶]
#title: "Sheet3"
#items: []
}
]
}
$data = Excel::load($path)->get();
if(!empty($data) && $data->count()){
dd($data);
foreach ($data as $key => $value) {
$insert[] = [
'name' => $value->name,
'email' => $value->email,
'phone' => $value->phone,
];
}}
我希望能够将所有数据作为数组放入 excel 工作表中,以便保存到我的数据库中。
解决方案
foreach ($data[0] as $key => $value) {
// dd($value->name);
$insert[] = [
'name' => $value->name,
'email' => $value->email,
'phone' => $value->phone,
];
}
推荐阅读
- haskell - Haskell 函数和柯里化
- flutter - 如何在firestore中增加数组中的单个元素以进行颤动
- python - 我在选定的列中有具有非唯一值的 pd.DataFrame。我怎样才能只留下所选列的唯一值的行?
- php - 如何在播种时使用 Laravel 7 生成唯一的外键?
- javascript - 在临时插件的输出中使用来自 webpack DefinePlugin 的值
- python - Google Cloud Dataflow 是否支持有状态处理?错误:工作流程失败
- python - 如何将 .img(磁盘映像)转换为 .jpg?
- openedge - 添加某些方法时如何在现有 Progress PASOE 应用程序中部署更改
- swift - Existential Container 与符合协议的结构实例之间的关系
- python - 在 python 中使用 Teradatasql 模块将数据上传到 Teradata 时出错