简体   繁体   中英

PHP convert content to separate arrays

I'm working on a fairly simple website where you can upload a file with some kind of info and the website analyses this data, returns certain values like averages, a bar plot etc. All is well with the ladder, but I'm struggling to read the info and convert it to arrays. In this case I have a .txt file with daily temperatures of December. I need to put these values into separate arrays, however whenever I use implode, the array is split simply into rows. Here's the var_dump;

 array(7) { 
[0]=> string(107) "0    1995    1996    1997    1998    1999    2000    2001    2002    2003    2004    2005    2006    2007    2008    2009    2010    2011    2012    2013    2014    2015 " 
[1]=> string(107) " 1   3.4 7.4 8.3 5.9 10.4    10.8    8.6 6.6 6.7 9.9 6.2 9.1 11  7   5.4 5.4 8.7 4   9.4 5.9 8.2 2   0.7 9.6 6.9 3.2 10.4" 
[2]=> string(107) " 9.3 6.5 4.1 7.8 7.2 5.7 0.3 8.6 4.5 2.6 6.6 8.6 8.2 9   7.2 10.4 3  1.6 10.9    0.9 4   10  7.1 10.7    2.1 7.4 4   7.7" 
[3]=> string(107) " -1.6    8.4 0.8 -0.3    8.9 5.6 6.5 9.5 8   10 4    -0.2    11.1    1.7 2.2 9.7 9.8 9.9 -0.2    9.6 6   8.8 1.5 5.5 1.4 -0.7    9." 
[4]=> string(107) "3    6.9 7   8.9 8.6 7.8 5   1.2 11.1    -0.1    6.8 8.5 9.2 7.8 0.1 8.9 9.5 8.5 -2.3    2.8 4.2 1   7.6 6.4 6.1 9   7.3 7.8 " 
[5]=> string(107) "6    7.2 9.6 1.1 4.8 4.8 6.4 6.1 3.7 6.7 7.8 9.7 2.2 1.7 3.8 1.8 7   5.8 6.7 8.8 8.5 5.4 7   1.6 9.9 6.6 3.4 5.2 " 
[6]=> string(107) "8.7  7.3 1.1 3.4 3.3 7.5 10  5   2.2 2.4 4.6 4.1 7.4 6.9 9.6 3.8 8   -1.6    7.9 9   3.1 5.2 9.6 6.3 1.2 3.1 1.6 7.5 " 
}

I need to skip whitespace & tabs when they're there and whenever there's a numerical or floating point, put them into the same array element. For example, we've got 4.5 2.7 5, I need 4.5 to be one element, then 2.7 to be the other, etc. I've tried using ifs with the conditions mentioned, however, it did not work. I feel like there's a small amount of syntax I'm not yet familiar with or maybe a function I overlooked.

Here's the clean code I've got so far;

<?php

$getcont = file_get_contents('C:\xampp\htdocs\gruodis.txt');
$contstr = str_split($getcont);

$a = 0;
$b = 0;
$c = 0;
$d = 0;
$sum = 0;
$contarr = array();

//fix the < values

for ($a = 0; $a < 7; $a++){
                for ($b = 0; $b < 107; $b++){
                    $contarr[$a][$b] = $contstr[$c];
                    $c++;                   
        }
        echo '<br>';
}
echo '<br><br>';

foreach ($contarr as $key => $value){
        $val = implode($value);
        $contarr[$key] = $val;
}

var_dump($contarr);

?>

Here's the content I'm using; https://pastebin.com/QgNzm6G8

Thanks in advance!

The next simple code use preg_split & array_map functions for split rows into number arrays:

$splitted = array_map(
    function($r) {
        return preg_split('/\s+/', $r);
    },
    $data
);

var_dump($splitted);

Test PHP code here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM