php array slice every 2th rule -


i have array on php outputs in txt file:

printing grid -- 1 values -- undef = -9.99e+08 20.2 \nprinting grid -- 1 values -- undef = -9.99e+08 102.0 \nprinting grid -- 1 values -- undef = -9.99e+08 55.1 \nprinting grid -- 1 values -- undef = -9.99e+08 -18.3 \n 

what point, need nummeric values @ beginning of each rule. 20.2, 102.0, 55.1, -18,3 etc... example, there can more or less in txt file.

now do? have tested array slice function can't exclude rule:

printing grid -- 1 values -- undef = -9.99e+08 

this array slice code...

$arraygood = array_slice($arraybad, 1, 4); 

and foreach loop create text file:

$file = fopen("array.txt","w"); foreach ($arraygood $meting => $waarde) { echo fwrite($file,$waarde . '\n'); } fclose($file); 

thanks!

as posted in comments, understanding have array of strings similar following:

$lines = array(     "printing grid -- 1 values -- undef = -9.99e+08",     "20.2 \nprinting grid -- 1 values -- undef = -9.99e+08",     "102.0 \nprinting grid -- 1 values -- undef = -9.99e+08",     "55.1 \nprinting grid -- 1 values -- undef = -9.99e+08",     "-18.3 \n" ); 

and want extract numeric value start of every string starts numeric value, , write value file.

if so, following 1 basic way achieve that:

// init array extracted numbers $numbers = array();  // loop on each line foreach ($lines $line) {     // numeric values suffixed newlines - explode string on newline     // set -1 limit exclude last element, in case should      // mean 1 value returned per exploded string     $tokens = explode("\n", $line, -1);      // skip empty arrays or ones null - should exclude      // 'header' line(s) have no newline char \n     if (0 !== count($tokens) && null !== $tokens[0]) {         // add numeric value new array         $numbers[] = $tokens[0];     } }  // done... can save values file fwrite() or file_put_contents() var_dump($numbers); 

should yield:

array (size=4)   0 => string '20.2 ' (length=5)    1 => string '102.0 ' (length=6)   2 => string '55.1 ' (length=5)   3 => string '-18.3 ' (length=6) 

hope helps :)

edit

online example: https://eval.in/138676

you can write array file little more succinctly doing like:

file_put_contents('array.txt', implode("\n", $numbers)); 

Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -