Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将不规则的CSV数据提取到结构化数组中-尝试使用PHP_Php_Arrays_Csv - Fatal编程技术网

将不规则的CSV数据提取到结构化数组中-尝试使用PHP

将不规则的CSV数据提取到结构化数组中-尝试使用PHP,php,arrays,csv,Php,Arrays,Csv,使用PHP尝试解决这个问题,但也可以使用其他解决方案(Python、Bash等) 我有以下格式的csv数据: 注意:门店名称id(第1行和第6行)始终为10位数字 产品id(col1,第2,3,4行和第7,8,9,10,11行)始终为7位数字 Store name 0123456789,,, 0123456,product desc,1,1.00 1234567,product desc2,1,2.00 2345678,product desc3,1,3.00 Ship Total,6.00,,

使用PHP尝试解决这个问题,但也可以使用其他解决方案(Python、Bash等)

我有以下格式的csv数据: 注意:门店名称id(第1行和第6行)始终为10位数字 产品id(col1,第2,3,4行和第7,8,9,10,11行)始终为7位数字

Store name 0123456789,,,
0123456,product desc,1,1.00
1234567,product desc2,1,2.00
2345678,product desc3,1,3.00
Ship Total,6.00,,
Store name2 9876543210,,,
0123456,product desc,4,1.00
1234567,product desc2,2,2.00
2345678,product desc3,1,3.00
3456789,product desc4,3,4.00
45678901,product desc5,1,5.00
Ship Total,28.00,,
所需格式为:

0123456789,0123456,product desc,1,1.00
0123456789,1234567,product desc2,1,2.00
0123456789,2345678,product desc3,1,3.00
9876543210,0123456,product desc,4,1.00
9876543210,1234567,product desc2,2,2.00
9876543210,2345678,product desc3,1,3.00
9876543210,3456789,product desc4,3,4.00
9876543210,45678901,product desc5,1,5.00
我有一个程序来解析上述格式的数据

我已将存储区放入一个数组,将事务放入另一个数组。。。只需要把它们放在一起

这是我到目前为止得到的

打印结果:

在回答后编辑。以下是最有效的方法

[守则]

$file = fopen($datafile, 'r'); 

$aT = array();
$LastStore = '';
while (($result = fgetcsv($file)) !== false) 
{ 
    if (preg_match('/\d{10}$/', $result[0],$store)) { 
        $LastStore = $store;
    } elseif (preg_match('/^\d{7}/', $result[0],$transaction)) { {
        $aT[] = array("Store"=>$LastStore, "Item"=>$transaction[0],"Desc"=>$transaction[1],"Qty"=>$transaction[2],"Price"=>$transaction[3])
    }
} 
fclose($file); 
[/code]

$handle = @fopen($datafile, "r");
if ($handle) {
    $Out = '';
    $Store = '';
    while (($buffer = fgets($handle)) !== false) {
        if (substr($buffer, 0, 5) == 'Store') {
            preg_match('/\d{10}/', $buffer, $storeId);
            $Store = $storeId[0] . ',';
        } else if (substr($buffer, 0, 4) == 'Ship') {
            // ignore
        } else {
            $Out .= $Store . $buffer . "\n";
        }
    }
}
fclose($handle);

file_put_contents('Results.txt', $Out);
或者,如果您希望所有内容都在一个大数组中


我建议这样做


谢谢你们两位。如果我有代表,你们都会得到我的道具。没问题!行吗?将其中一个标记为可接受的答案,以获得2个代表点。关闭呼叫,但看起来你可以比Robbie使用代表多一点。再次感谢两位。上面的更新显示了我使用的最终代码。谢谢,如果可以的话,我会代表你们两个。结合您的想法,它的工作完美。
$file = fopen($datafile, 'r'); 

$txns = array();
$LastStore = '';
while (($result = fgetcsv($file)) !== false) 
{ 
    if (preg_match('/\d{10}$/', $result[0],$store)) { 
        $LastStore = $store;
    } elseif (preg_match('/[A-Za-z]+/', $result[0])) {
        continue;
    } else {
        $txns[] = array("Store"=>$LastStore[0], "Item"=>$result[0],"Desc"=>$result[1],"Qty"=>$result[2],"Price"=>$result[3]);
    }
} 
fclose($file);
$file = fopen($datafile, 'r'); 

$Out = '';
$LastStore = '';
while (($result = fgetcsv($file)) !== false) 
{ 
    if (preg_match('/\d{10}$/', $result[0],$store)) { 
        $LastStore = $store;
    } elseif (preg_match('/^\d{7}/', $result[0],$transaction)) { {
        $aT = array("Store"=>$LastStore, "Item"=>$transaction[0],"Desc"=>$transaction[1],"Qty"=>$transaction[2],"Price"=>$transaction[3])
        $Out .= implode(',', $aT) . "\n";

    }
} 
fclose($file); 

// Output as file (if this is as intended). Otherwise you have $Out to be the CSV string you need.    
file_put_contents($OutFileName, $Out);
$file = fopen($datafile, 'r'); 

$aT = array();
$LastStore = '';
while (($result = fgetcsv($file)) !== false) 
{ 
    if (preg_match('/\d{10}$/', $result[0],$store)) { 
        $LastStore = $store;
    } elseif (preg_match('/^\d{7}/', $result[0],$transaction)) { {
        $aT[] = array("Store"=>$LastStore, "Item"=>$transaction[0],"Desc"=>$transaction[1],"Qty"=>$transaction[2],"Price"=>$transaction[3])
    }
} 
fclose($file); 
$handle = @fopen($datafile, "r");
if ($handle) {
    $Out = '';
    $Store = '';
    while (($buffer = fgets($handle)) !== false) {
        if (substr($buffer, 0, 5) == 'Store') {
            preg_match('/\d{10}/', $buffer, $storeId);
            $Store = $storeId[0] . ',';
        } else if (substr($buffer, 0, 4) == 'Ship') {
            // ignore
        } else {
            $Out .= $Store . $buffer . "\n";
        }
    }
}
fclose($handle);

file_put_contents('Results.txt', $Out);