猿问

导出 CSV 不能使用 stdClass 类型的对象作为数组

我想将数据从我的 json 导出到 csv 文件,但我收到此错误无法使用 stdClass 类型的对象作为数组


我想知道如何将它用作数组


public function exportUsers()

    {

        $users = ServicePoint::all()->where("nature", "SP")->toArray();

        $users = ServicePoint::all()->where('statut','<>', 2);


        $arrayCsv = [];

        foreach ($users as $key => $line){

            $arrayCsv[$key][] = $line['name'];

            $arrayCsv[$key][] = $line['lastname'];

            $arrayCsv[$key][] = $line['email'];


        

        }

有什么可以帮忙的


人到中年有点甜
浏览 130回答 3
3回答

胡说叔叔

作为它的对象,它可以被->操作符访问foreach ($users as $key => $line) {&nbsp; &nbsp; $arrayCsv[$key][] = $line->name;&nbsp; &nbsp; $arrayCsv[$key][] = $line->lastname;&nbsp; &nbsp; $arrayCsv[$key][] = $line->email;}应该管用。

BIG阳

我假设您正在尝试根据 2 个where子句获取用户。如果是这样,试试这个$users = ServicePoint::all()->where("nature", "SP")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->where('statut','<>', 2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->get();然后更改这些行$arrayCsv[$key][] = $line->name;$arrayCsv[$key][] = $line->lastname;等等。

紫衣仙女

public function exportUsers() {&nbsp; &nbsp; $users = ServicePoint::where("nature", "SP")->where('statut','<>', 2)->all()->toArray();&nbsp; &nbsp; $arrayCsv = [];&nbsp; &nbsp; foreach ($users as $key => $line){&nbsp; &nbsp; &nbsp; &nbsp; $arrayCsv[$key][] = $line['name'];&nbsp; &nbsp; &nbsp; &nbsp; $arrayCsv[$key][] = $line['lastname'];&nbsp; &nbsp; &nbsp; &nbsp; $arrayCsv[$key][] = $line['email'];&nbsp; &nbsp; }}试试这个,它应该可以工作。
随时随地看视频慕课网APP
我要回答