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(n²)所以这是一个糟糕的方法。 这是使用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
...