日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php excel转数组,php将excel数据转换为3d数组(php convert excel data into 3d array)

發布時間:2024/3/26 php 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php excel转数组,php将excel数据转换为3d数组(php convert excel data into 3d array) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

php將excel數據轉換為3d數組(php convert excel data into 3d array)

我有一個excel文件,其數據是一個3d數組。

我正在使用PHPExcel Object將數據返回到3d數組中。 但我只能返回一個二維數組。

$objPHPExcel = PHPExcel_IOFactory::load($file);

$cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();

foreach ($cell_collection as $cell) {

$column = $objPHPExcel->getActiveSheet()

->getCell($cell)

->getColumn();

$row = $objPHPExcel->getActiveSheet()

->getCell($cell)

->getRow();

$data_value = $objPHPExcel->getActiveSheet()

->getCell($cell)

->getValue();

if ($row == 1) {

$header[$row][$column] = $data_value;

} else {

$arr_data[$row][$column] = $data_value;

}

}

如何返回如下結果數組:

$res = array(

[2]=>array(

[A] => A,//column A

[B] =>array([1]=>'red',[2]=>'pink',[3]=>'purple')

),

[3]=>array(

[A] => B,//column B

[B] =>array([1]=>'white',[2]=>'blue',[3]=>'black')

),

);

任何人都知道如何修改我的代碼以返回預期的結果? 任何答案將不勝感激!

I have an excel file whose data is a 3d array.

I am using PHPExcel Object to return the data into 3d array. But I can only return a 2d array.

$objPHPExcel = PHPExcel_IOFactory::load($file);

$cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();

foreach ($cell_collection as $cell) {

$column = $objPHPExcel->getActiveSheet()

->getCell($cell)

->getColumn();

$row = $objPHPExcel->getActiveSheet()

->getCell($cell)

->getRow();

$data_value = $objPHPExcel->getActiveSheet()

->getCell($cell)

->getValue();

if ($row == 1) {

$header[$row][$column] = $data_value;

} else {

$arr_data[$row][$column] = $data_value;

}

}

How can I return a result array like this:

$res = array(

[2]=>array(

[A] => A,//column A

[B] =>array([1]=>'red',[2]=>'pink',[3]=>'purple')

),

[3]=>array(

[A] => B,//column B

[B] =>array([1]=>'white',[2]=>'blue',[3]=>'black')

),

);

Anyone knows how I could modify my code to return the expected result? Any answer would be appreciated!

原文:https://stackoverflow.com/questions/46231887

更新時間:2020-01-07 09:46

最滿意答案

我不是一個PHP程序員,這個代碼沒有遵守。 在這里,我添加了我的代碼來生成您期望的數組。 它可以幫助您解決問題。 我認為$ column是一個像A,B這樣的列值.....

$i = 1;

$i_str = "";

$res = array();

foreach ($cell_collection as $cell) {

$column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();

$row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();

$data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();

if ($row == 1) {

$header[$row][$column] = $data_value;

} else {

if($column == 'A' && $data_value != ""){

$i++;

$i_str = (string) $i;

$res[$i_str]['A'] = $data_value;

$res[$i_str]['B'] = array();

}

else if($column == 'B'){

$len = (string) (count($res[$i_str]['B']) + 1);

$res[$i_str]['B'][$len] = $data_value;

}

}

}

print_r($res) // here you will get your expected array

I am not a php programmer and this code not complied. Here I have added my code to generate your expected array. It can help you to solve your problem. I think $column is a column value like A,B.....

$i = 1;

$i_str = "";

$res = array();

foreach ($cell_collection as $cell) {

$column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();

$row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();

$data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();

if ($row == 1) {

$header[$row][$column] = $data_value;

} else {

if($column == 'A' && $data_value != ""){

$i++;

$i_str = (string) $i;

$res[$i_str]['A'] = $data_value;

$res[$i_str]['B'] = array();

}

else if($column == 'B'){

$len = (string) (count($res[$i_str]['B']) + 1);

$res[$i_str]['B'][$len] = $data_value;

}

}

}

print_r($res) // here you will get your expected array

2017-09-15

相關問答

由于所有Ct的長度都不相同,因此除了重建新塊之外別無選擇。 但是使用data[data['ct'] == ct]可能是O(n2)所以這是一個糟糕的方法。 這是使用Panel的解決方案。 cumcount重新編號每個Ct行: t=5

CFt=randint(0,t,(40*t,6)).astype(float) # 2D data

df= pd.DataFrame(CFt)

df2=df.set_index([df[0],df.groupby(0).cumcount()]).sort_index()

...

這不是關于如何創建和填充這些值的3D數組的完整討論,而只是指出您所呈現的代碼中的錯誤位置,這只是創建一行。 我懷疑你的錯誤出現在Global或Dim MonthArray語句中。 在VBA中,數組沒有“命名”參數。 如果這是問題,您可以嘗試以下方法: Global MonthArray(0 to 2) as Variant

然后,在你的代碼中: MonthArray(0) = (ActiveSheet.Cells(i + 1, 9).value)

MonthArray(1) = (ActiveS

...

如果您使用的是Windows,則可以使用Brekel的工具 (查看此工作流程教程 ) 如果您可以編寫一些代碼,也可以嘗試這種方法 。 If you're on Windows you can use Brekel's tools (have a look at this workflow tutorial) If you can write a bit of code, you can also try this approach.

我不是一個PHP程序員,這個代碼沒有遵守。 在這里,我添加了我的代碼來生成您期望的數組。 它可以幫助您解決問題。 我認為$ column是一個像A,B這樣的列值..... $i = 1;

$i_str = "";

$res = array();

foreach ($cell_collection as $cell) {

$column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();

...

沒有一種方法可以轉換成一個mysql對象而無需寫入tmp表并將其讀回。以下將處理sql語句或數組轉換,然后根據問題無需轉換。 $all_rows = $notify->notification_sql(); // This either returns an SQL statement ready to run OR a set of rows in an array

if (!is_array($all_rows)) { // If it's not an array, run the SQL

...

pixelspace = reader.GetPixelSpacing()

spacing = image.GetSpacing()

vtk_data = image.GetPointData().GetScalars()

numpy_data = numpy_support.vtk_to_numpy(vtk_data)

numpy_data = numpy_data.reshape(dims[0], dims[1], dims[2])

numpy_d

...

嘗試將三個列表作為元組傳遞: A = numpy.array((X, Y, Z), dtype=float)

在numpy.array文檔中, numpy.array的簽名是 numpy.array(object,dtype = None,copy = True,order = None,subok = False,ndmin = 0,maskna = None,ownmaskna = False) 即單個參數object是變成ndarray的東西,每個其他參數必須是一個關鍵字參數(因此你得到的

...

如果這是一個數字矩陣,那么它應該只需要42000*784 *10 == 329280000字節。 如果它是一個字符向量,它可能會更大,具體取決于基礎值中唯一項的數量。 我確實嘗試使用“[r]矩陣重新維度”上的搜索找到重復但沒有成功,盡管我確實找到了一個關于用dim

...

這適用于您的示例,我希望它對您來說足夠通用: gb

identical(ga, gb)

# [1] TRUE

I also found this way using the package abind: abind( split(gdf, gdf$X1), along=3)

In [54]: arr = np.array([[[ 1., 5., 4.],

[ 1., 5., 4.],

[ 1., 2., 4.]],

[[ 3., 6., 4.],

[ 6., 6., 4.],

[ 6., 6., 4

...

總結

以上是生活随笔為你收集整理的php excel转数组,php将excel数据转换为3d数组(php convert excel data into 3d array)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。