首页 > 解决方案 > 如何访问从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 工作表中,以便保存到我的数据库中。

标签: phparrayslaravel

解决方案


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

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

推荐阅读