php excel转数组,php将excel数据转换为3d数组(php convert excel data into 3d array)
php將excel數(shù)據(jù)轉(zhuǎn)換為3d數(shù)組(php convert excel data into 3d array)
我有一個(gè)excel文件,其數(shù)據(jù)是一個(gè)3d數(shù)組。
我正在使用PHPExcel Object將數(shù)據(jù)返回到3d數(shù)組中。 但我只能返回一個(gè)二維數(shù)組。
$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;
}
}
如何返回如下結(jié)果數(shù)組:
$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')
),
);
任何人都知道如何修改我的代碼以返回預(yù)期的結(jié)果? 任何答案將不勝感激!
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
更新時(shí)間:2020-01-07 09:46
最滿意答案
我不是一個(gè)PHP程序員,這個(gè)代碼沒(méi)有遵守。 在這里,我添加了我的代碼來(lái)生成您期望的數(shù)組。 它可以幫助您解決問(wèn)題。 我認(rèn)為$ column是一個(gè)像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
相關(guān)問(wèn)答
由于所有Ct的長(zhǎng)度都不相同,因此除了重建新塊之外別無(wú)選擇。 但是使用data[data['ct'] == ct]可能是O(n2)所以這是一個(gè)糟糕的方法。 這是使用Panel的解決方案。 cumcount重新編號(hào)每個(gè)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()
...
這不是關(guān)于如何創(chuàng)建和填充這些值的3D數(shù)組的完整討論,而只是指出您所呈現(xiàn)的代碼中的錯(cuò)誤位置,這只是創(chuàng)建一行。 我懷疑你的錯(cuò)誤出現(xiàn)在Global或Dim MonthArray語(yǔ)句中。 在VBA中,數(shù)組沒(méi)有“命名”參數(shù)。 如果這是問(wèn)題,您可以嘗試以下方法: 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.
我不是一個(gè)PHP程序員,這個(gè)代碼沒(méi)有遵守。 在這里,我添加了我的代碼來(lái)生成您期望的數(shù)組。 它可以幫助您解決問(wèn)題。 我認(rèn)為$ column是一個(gè)像A,B這樣的列值..... $i = 1;
$i_str = "";
$res = array();
foreach ($cell_collection as $cell) {
$column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
...
沒(méi)有一種方法可以轉(zhuǎn)換成一個(gè)mysql對(duì)象而無(wú)需寫入tmp表并將其讀回。以下將處理sql語(yǔ)句或數(shù)組轉(zhuǎn)換,然后根據(jù)問(wèn)題無(wú)需轉(zhuǎn)換。 $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
...
嘗試將三個(gè)列表作為元組傳遞: 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) 即單個(gè)參數(shù)object是變成ndarray的東西,每個(gè)其他參數(shù)必須是一個(gè)關(guān)鍵字參數(shù)(因此你得到的
...
如果這是一個(gè)數(shù)字矩陣,那么它應(yīng)該只需要42000*784 *10 == 329280000字節(jié)。 如果它是一個(gè)字符向量,它可能會(huì)更大,具體取決于基礎(chǔ)值中唯一項(xiàng)的數(shù)量。 我確實(shí)嘗試使用“[r]矩陣重新維度”上的搜索找到重復(fù)但沒(méi)有成功,盡管我確實(shí)找到了一個(gè)關(guān)于用dim
...
這適用于您的示例,我希望它對(duì)您來(lái)說(shuō)足夠通用: 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
...
總結(jié)
以上是生活随笔為你收集整理的php excel转数组,php将excel数据转换为3d数组(php convert excel data into 3d array)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ab命令压测
- 下一篇: laravel5.8(二十一)larav