php simple array question

alexho

New Member
Reaction score
0
i have a quick question about php functions for the specific purpose of loading files into an array. I do not use file() as it was greatly reduced in speed in newer vers i use fgets btw..
Question: calling functions to get a line like fgets goes quicker on lines that have less data?.. should be obvious or??
and another question to ponder
Question: it would be nice if there was a way to get lines that are not from the start of a file, but now it is impossible to for example load a text file like this:
Code:
apples
carrots
bananas
pears
where the pointer goes to array 2 bananas and downwards. i have yet to seen this to be possible, so i guess thats why im asking a question about it. it would be amazing if developers or an existing function exists to get a file into an array at midway line or whichever line and downwards.. :rolleyes:
 

enouwee

Non ex transverso sed deorsum
Reaction score
240
It doesn't exist, because it's straightforward to write. PHP is already bloated enough, it doesn't need a built-in function for anything its lazy developers dream of.

The brain-dead one-liner way:
PHP:
<?php

print_r(array_slice(file('test'), 1, 9));

?>

Or the I-am-able-to-write-it-the-more-efficient way:
PHP:
<?php

function fmid($filename, $start = 0, $end = -1)
{
  if ($start < 0)
  {
    return false;
  }

  if ($end >= 0)
  {
    if ($end < $start)
    {
      return false;
    }
    else
    {
      $end -= $start;
    }
  }

  $fh = fopen($filename, 'r');
  if (!is_resource($fh))
  {
    return false;
  }

  $rv = array();
  for($i = 0; $i < $start; $i++)
  {
    if (fgets($fh) === false)
    {
      fclose($fh);
      return false;
    }
  }

  if ($end < 0)
  {
    while(!feof($fh))
    {
      if (($line = fgets($fh)) === false)
      {
        if (!feof($fh))
        {
          $rv = false;
        }

        break;
      }
      $rv[] = $line;
    }
  }
  else
  {
    for ($i = 0; $i <= $end; $i++)
    {
      if (($line = fgets($fh)) === false)
      {
        if (!feof($fh))
        {
          $rv = false;
        }

        break;
      }
      $rv[] = $line;
    }
  }

  fclose($fh);

  return $rv;
}

?>
 

alexho

New Member
Reaction score
0
yes that would work 'array_slice' ive seen it before :banghead: idiot (me) thanks :rolleyes:
i would have to agree n2lbr with replace \n :) not sure which is quicker but however this would be just for slow a file slowly crept through not in fgets
 

alexho

New Member
Reaction score
0
what exactly does fgets limiter do, count bytes per line, to the specified #, then go to new line? obviously this is not true, what is the real 'purpose' of limit anyway, I cannot seem to find much info on it. in depth for stuff is there any place that has alot of info, didnt see much in php.net?


here something more simple i am trying to accomplish, i guess i didnt explain well enough. below is obviously a cookie cutter, but works but since im looking for true efficiency it is dumb because it would keep grabbing from the file never the less it just waits till the number is right. a method of not counting it but starting at start seems hard i would just need to get fgets to return false continuously eh
Code:
function astart($filename,$start) {
$x=0;
$xx = fopen ($filename, 'r');
while (!feof ($xx)) {
if ($x<$start && $x>=0) {
$buffer = fgets($xx);
echo "$x, $buffer :";
unset($buffer);
// '<script>alert("x is '.$x.' less than start, '.$buffer.'");</script>';

}else{
$buffer = fgets($xx, 4096);
}
      if ($x>=$start) {   

   if ($buffer!="" || !empty($buffer)) {
   //$faa[] = $buffer;
   $startfromstart.=$buffer;
   }
      }
   $x++;
}
fclose ($xx);

return $startfromstart;

}
for example i have lines that have a constant of NO more than 12 each line
i would do fgets(file, 13); since its -1 then it goes to the next line? i have read it and tried it but it seems not to work. there is some overseen flaw i dont see.
TO most simply 'say' this if i have very small lines that are hardly anything in size but at a certain length no more wouldnt it be better to set a smaller value? and what are those best values to use for smaller horizontal lines?
 

enouwee

Non ex transverso sed deorsum
Reaction score
240
It returns a zero-terminated string of up to n-1 bytes unless a newline is encountered.

Example: you have a file with the following content: "0123456789abcdefghijABC"

Code:
fgets($the_file, 11);
-> "0123456789"
fgets($the_file, 11);
-> "abcdefghij"
fgets($the_file, 11);
-> "ABC"

If you want the whole line for sure, just omit the length argument.
 

alexho

New Member
Reaction score
0
ok thanks for that then, it grabs the different characters. i will just use no length
if need the whole line it works just as quickly with no limiter as it is optional? i see 1024, 4096 or so most often. it said in php.net to use 1024 instead of 4096 for small lines. but w/o a limiter there isnt an amount it goes to the very end of the line. so really speed isnt even effected for having no limiter since it would keep going even for smaller lines, as with bigger lines?
 

enouwee

Non ex transverso sed deorsum
Reaction score
240
The second argument (length) defaults to 1024 if you don't set a value yourself. If your lines are longer than that, PHP will waste time increasing the buffer.
 

alexho

New Member
Reaction score
0
ok thank you. thats what i suspected with my line maximums its under 1024 when i set them to default my page loaded 0.07 with smaller values it works the same with 0.03-4 it works more efficiently in my case with smaller lines.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top