Php 从列表中创建三维阵列

Php 从列表中创建三维阵列,php,Php,我有以下清单: 1945/01/05 BA 87 34 1 59 50 1945/01/05加利福尼亚州1817454982 1945/01/13BA 6616148 1945/01/13加利福尼亚州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]

我有以下清单:

1945/01/05 BA 87 34 1 59 50
1945/01/05加利福尼亚州1817454982
1945/01/13BA 6616148
1945/01/13加利福尼亚州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);创建阵列,但如何从该列表创建三维阵列


感谢您的帮助

尝试以下方法:

更新了代码,以便您也可以加载文件,因为您有内存问题。 请考虑:如果加载的数据量已经造成内存问题,那么最终结果(数组)也可能会产生内存问题!因为这是更多的数据

因此,这将读取每行,所以它不会直接加载内存中的总文件。但由于内存问题,您可能不应该写入数组,而应该写入数据库(例如)。否则,内存问题将继续出现

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


与代码中的问题相同,最深数组的键不是0-4,而是0,2,4,6,8。请参阅我文章中的数组值。是的,现在您的索引是正确的。正如您在我的示例中看到的,不需要对计数器和索引执行很多操作,因为它将由PHP数组自动排序。因此,它将节省大量代码和计数器,从而增加复杂性并增加出现bug的机会。但是,是的,代码现在正在工作,并根据问题输出它应该做什么。致命错误:允许的内存大小为134217728字节,在file.php的$fields=explode(“”,$line)行上用尽(试图分配35字节)。它可以用$fields=preg_split('/\s+/',$line)替换该行,但现在数组又有一个索引(5而不是4). 为什么?你的档案有多大?这是没有问题的。如果它非常大,你需要一种不同的方法,每行读取一个文件,而不是在内存中一行读取一个巨大的文件。添加了一个新版本,它还允许从文件加载(加载文件时检查安全性!)。因为你已经有了128MB的内存限制,我想你的文件太大了。也许你应该问另一个问题,或者扩展这个问题,关于如何处理大量数据。在大多数情况下,在内存中加载全部不是正确的解决方案。preg_split工作得很好,唯一的问题是数组有一个空白数组
<?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);
<?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>';
?>