1

次のリストがあります。

1945/01/05 BA 87 34 1 59 50
1945/01/05 CA 18 17 45 49 82
1945/01/13 BA 6 66 1 16 48
1945/01/13 CA 40 60 32 50 80

等々....

次のような配列として配置したいと思います。

$array['BA'][0][0] = 87;
$array['BA'][0][1] = 34;
$array['BA'][0][2] = 1;
$array['BA'][0][3] = 59;
$array['BA'][0][4] = 50;

$array['CA'][0][0] = 18;
$array['CA'][0][1] = 17;
$array['CA'][0][2] = 45;
$array['CA'][0][3] = 49;
$array['CA'][0][4] = 82;

$array['BA'][1][0] = 6;
$array['BA'][1][1] = 66;
$array['BA'][1][2] = 1;
$array['BA'][1][3] = 16;
$array['BA'][1][4] = 48;

$array['CA'][1][0] = 48;
$array['CA'][1][1] = 60;
$array['CA'][1][2] = 32;
$array['CA'][1][3] = 50;
$array['CA'][1][4] = 80;

preg_split('/\s+/', $list); を使用できることはわかっています。配列を作成するには、どうすればこのリストから 3D 配列を作成できますか?

ご協力ありがとうございました

4

2 に答える 2

3

次のようなものを試してください。

メモリの問題があるため、ファイルをロードできるようにコードを更新しました。考慮してください:データの量をロードすることですでにメモリの問題が発生している場合、最終結果である配列もメモリの問題を生成する可能性があります!それはさらに多くのデータなので!

したがって、これは1行ごとに読み取られるため、ファイル全体がメモリに直接読み込まれることはありません。ただし、メモリの問題があるため、たとえば、配列ではなくデータベースに書き込む必要があります。そうしないと、メモリの問題が発生し続けます。

<?PHP

/*
 * Example when small amount of data is in a string
 */

/*
//your input
$str='1945/01/05 BA 87 34 1 59 50
1945/01/05 CA 18 17 45 49 82
1945/01/13 BA 6 66 1 16 48
1945/01/13 CA 40 60 32 50 80';

//we work per line
$lines=explode("\n", $str);

//loop each line
foreach($lines AS $line) {

*/

/*
 * Example when big amount of data is in a file
 */
//this will contain the end result
$result=array();

$filename='lines.txt'; //contains the data like in your question

$fp = fopen($filename,'r');
while($line=fgets($fp)) {
    //explode each line on space so we get the different fields
    $fields=explode(' ', $line);

    //we remove the date, not needed
    unset($fields[0]);

    //we get the key (BA/CA/etc) and remove it also
    $key=$fields[1];
    unset($fields[1]);

    //we write the result to the array
    //using array_values so the indexes are from 0-4 again
    //because we removed items
    $result[$key][]=array_values($fields);

}


fclose($fp);

//show the result in html
echo '<pre>';
print_r($result);

質問が変更されました

これは、実際の質問ではなく、コメントで尋ねられた質問に答えます。

<?PHP

/*
 * Example when big amount of data is in a file
 */
//this will contain the end result
$result=array();

$filename='lines.txt'; //contains the data like in your question

$fp = fopen($filename,'r');
while($line=fgets($fp)) {
    //explode each line on space so we get the different fields
    $fields=explode(' ', $line);

    //we remove the date, not needed
    unset($fields[0]);

    //we get the key (BA/CA/etc) and remove it also
    $key=$fields[1];
    unset($fields[1]);

    //We start counting the numbers
    foreach($fields AS $nr) {
        $nr=trim($nr);
        if(empty($result[$key][$nr])) {
            $result[$key][$nr]=1;
        }else{
            $result[$key][$nr]++;
        }
    }

}


fclose($fp);

//show the result in html
echo '<pre>';
print_r($result);
于 2012-07-21T10:33:22.123 に答える
0
<?php
$a = '1945/01/05 BA 87 34 1 59 50
1945/01/05 CA 18 17 45 49 82
1945/01/13 BA 6 66 1 16 48
1945/01/13 CA 40 60 32 50 80';

$array = array();
$list = explode("\n", $a);
echo '<pre>';
print_r($list);
echo '</pre>';
$first_index = 0;
$counter = 0;
foreach($list as $key=>$value) {
    if($counter >=2) {
        $first_index++;
        $counter= 0;
    }
    $row = explode(" ",$value);
    $k = $row[1];

    for($i=2; $i< count($row); $i++) {      
        $array[$k][$first_index][] = $row[$i];          
    }
    $counter++;
}
echo '<pre>';
print_r($array);
echo '</pre>';
?>
于 2012-07-21T10:53:00.247 に答える