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 に答える