如何访问从Excel文件生成的复杂数组

时间:2019-05-11 20:08:01

标签: php arrays laravel

我从excel文件中生成了数组,我想访问该数组,但出现此错误:此集合实例上不存在属性[name]。

我已经可以从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工作表中,以便将其保存到数据库中。

1 个答案:

答案 0 :(得分:0)

foreach ($data[0] as $key => $value) {
      // dd($value->name);

     $insert[] = [
     'name' => $value->name,
     'email' => $value->email,
     'phone' => $value->phone,
     ];
 }