我有一个 csv 文件列表,我需要 php 将其转换为一个大型多维数组,仅使用 2 个 php 进行测试超过了进行第二次转换时的最大执行时间。
第一次转换是从 csv 到数值多维数组
第二次转换是从数值多维数组到字符串和数值多维数组,这是 php 产生错误的地方。
我的代码如下:
function parse($data) {
$i = 0;
while (isset($data[$i])) {
$row = 0;
while (isset($data[$i][$row])) {
switch ($row) {
case 0:
//General Item details
$fOv = 0;
while (isset($data[$i][$row][$fOv])) {
$array['Item'][$i][$data[$i][row][$fOv]] = $data[$i][$row + 1][$fOv];
$fOv++; // THIS IS THE PLACE OF THE ERROR
}
break;
case 2:
//General Transaction details
$fOv = 0;
while (isset($data[$i][$row][$fOv])) {
$array['Transaction'][$i][$data[$i][$row][$fOv]] = $data[$i][$row + 1][$fOv];
$fOv++;
}
break;
case 4:
//Buyer details
$fOv = 0;
while (isset($data[$i][$row][$fOv])) {
$array['Transaction']['Buyer'][$i][$data[$i][$row][$fOv]] = $data[$i][$row + 1][$fOv];
$fOv++;
}
break;
case 6:
//Shipping details
$fOv = 0;
while (isset($data[$i][$row][$fOv])) {
$array['Transaction']['Shipping'][$i][$data[$i][$row][$fOv]] = $data[$i][$row + 1][$fOv];
$fOv++;
}
break;
case 8:
//Status details
$fOv = 0;
while (isset($data[$i][$row][$fOv])) {
$array['Transaction']['Status'][$i][$data[$i][$row][$fOv]] = $data[$i][$row + 1][$fOv];
$fOv++;
}
break;
}
$row + 2;
}
$i++;
}
return $array;
}
这是数值数组的样子:
array (
0 => array (
0 => array (
0 => 'field 1', 1 => 'field 2', 2 => 'field 3' //continues to field 17
)
1 => array (
0 => 'value 1', 1 => 'value 2', 2 => 'value 3' //continues to value 17
//these values are for the fields in array[0][0]
)
2 => array ( //same as array[0][0] )
3 => array ( //same as array[0][1], values for array[0][2] )
4 => array ( //same as array[0][0] )
5 => array ( //same as array[0][1], values for array[0][4] )
6 => array ( //same as array[0][0] )
7 => array ( //same as array[0][1], values for array[0][6] )
8 => array ( //same as array[0][0] )
9 => array ( //same as array[0][1], values for array[0][8] )
)
1 => array (
//same structure as array[0], different details
)
)
我试图为字符串和数字数组获取什么:
array (
0 => array (
'Item' => array (
0 => array (
'field 1' => 'value 1'
'field 2' => 'value 2'
//continues to field 17
)
)
'Transaction' => array (
0 => array (
'field n' => 'value n' //repeated until field 17
'Buyer' => array (
//same as above in array[0]['Item'][0]
)
'Shipping' => array (
//same as above in array[0]['Item'][0]
)
'Status' => array (
//same as above in array[0]['Item'][0]
)
)
)
)
1 => array (
//same structure as array[0], different values
)
)
非常感谢任何帮助,在此先感谢。
编辑: 我不想增加最大执行时间,脚本应该花费不到 30 秒,与我首先将数据存储在 csv 文件中的工作脚本相同。