0

使用 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

我有一个程序来解析上述格式的数据。

我已经将商店放入一个数组中,并将交易放入另一个数组中......只需将它们放在一起。

这是我到目前为止所得到的。

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

while (($result = fgetcsv($file)) !== false)
{
    $csv[] = $result;
}

fclose($file);

foreach ($csv as $key => $value) { 
    if (preg_match('/\d{10}$/', $value[0],$store)) {
        $stores[$key] .= $store[0];
    }
}
print_r($stores);

foreach ($csv as $key => $value) {
    if (preg_match('/^\d{7}/', $value[0],$transaction)) {
        $transactions[$key] = array("Item"=>$value[0],"Desc"=>$value[1],"Qty"=>$value[2],"Price"=>$value[3]);
    }
}
print_r($transactions)

print_r 结果:

Array
(
    [0] => 0123456789
    [5] => 9876543210
)

Array
(
    [1] => Array
        (
            [Item] => 0123456
            [Desc] => product desc
            [Qty] => 1
            [Price] => 1.00
        )
...
... arrays 2,3,4,6,7,8,9....
...

    [10] => Array
        (
            [Item] => 45678901
            [Desc] => product desc5
            [Qty] => 1
            [Price] => 5.00
        )
)

回答后编辑。这是完美的工作。

[代码]

$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);

[/代码]

4

2 回答 2

0
$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); 
于 2012-05-15T22:48:11.593 回答
0

我会建议这样的事情

$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);
于 2012-05-15T23:19:26.220 回答