1

i'm trying to put input (below) into multiple arrays (or maybe its simpler into one array) to finally export it into mysql table, input data goes like:

0 98
77 09
0 12
0 98234

32 0
0 1 
0 0
345 32

34 9
6437 34
789 0
0 0

.
.

34 0

my simple code ($run_txt_filter1 is input):

if ( $counted == 64)
{
    echo "line's number:  ".$counted;
    //echo $run_txt_filter1;
    for ($modi = 0; $modi <= 15; $modi++)
    {
        for ($simhi = 1; $simhi <= 4 ; $simhi++)
        {
                $exploded=explode(" ", $run_txt_filter1);
                var_dump($exploded)." \n";
        }

    } 

}

Why var_dump keeps saying the id from 0-64 ? (there always should be 64 input lines). What a really want to achieve is:

array0=(0, 77, 0, 0)
array1=(98, 09, 12, 98234)
array2=(32, 0, 0, 345)
.
.
array30=(0, 12, 0, 34)
array31=(0, 0, 0, 0)

thanks in advance

7
  • Where is $counted's value set? Commented Dec 5, 2011 at 22:40
  • $counted is only for checking if $run_txt_filter1 has 64 lines of data. It's set just before 'if' control Commented Dec 5, 2011 at 22:44
  • why not just make it separated by comma instead of white space? then explode it into 1 array instead of multiple arrays (that would be easier and simpler to manipulate anyway I think your just going to export all those going to your database) just like what you would like to happen? Commented Dec 5, 2011 at 22:49
  • well i thought it might be simpler to loop on it to achieve multiple arrays (which i can easly put into mysql) cause a i need to enumerate(give id for separate arrys - as in example of my whishing output) those values in order to have them 'sorted' in database :) Commented Dec 5, 2011 at 22:54
  • I see I think you could use multi-dimensional array instead. :) Commented Dec 5, 2011 at 22:59

3 Answers 3

1

try to separate your explode for the next line and explode for the white space. cause on your code above it's reading the whole string as one causing your program to store it on a single array. so it would be a nested loop by then. :)

Sign up to request clarification or add additional context in comments.

Comments

0

Considering this input:

$in = <<<IN
0 98
77 09
0 12
0 98234

32 0
0 1 
0 0
345 32

34 9
6437 34
789 0
0 0
IN;

This algorithm solves your problem i think:

$final_array = array();
$offset = 0;
foreach(preg_split("/(\r?\n){2,}/",$in) as $block){
    foreach(preg_split("/(\r?\n)/",$block) as $line){
        foreach(preg_split("/\s+/",$line) as $column => $value){
            if($value=='') continue;
            if(!isset($final_array[$offset+$column])) 
                $final_array[$offset+$column] = array();
            $final_array[$offset+$column][]=$value;
        }
    }
    $offset = count($final_array);
}
print_r($final_array);

Comments

0

try something similar to this one I'm not sure if this would work:

inputs:

$inputs = "0 98
77 09
0 12
0 98234

32 0
0 1 
0 0
345 32

34 9
6437 34
789 0
0 0

.
.

34 0";

code:

$run_txt_filter1 = explode("\n", $inputs);

if(count($run_txt_filter1) == 64)
{
 foreach($run_txt_filter1 as $input)
 {
  $exploded = explode(' ', $input);
  print_r($exploded);
 }
}

The "\n" is the next line for windows but it's different on linux its "\r" I'm not sure if it would work you may also try the combination as

explode("\r\n", $inputs); 

but just a try if the next line won't work I think you could use some other way to separate each set of values either user other type of characters like ',',';',':' etc. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.