Hello.

Such code:

foreach($array as $key=>$el) { fseek($r, 3, SEEK_CUR); fputs($r, $el, 3); } 

Writes data to the file, if the item is less than 3 bytes - the rest is filled, I don’t know how to say it correctly, but probably with zero bytes. If you open a file in Sublime Text:

 3135 3400 3535 0000 //здесь зашифровано 154 и 55, между ними заполняется 0 

The fact is that the array is very large, tens of thousands of records, it is necessary to drive the pointer and record tens of thousands of times. Is there a way to write from an array to a string in the same way? It is important for me that the data be of a certain length. Those. even if the element is 1 character (1 byte), then it should occupy the same 3 bytes.

Update: Task description: I have an array of numbers that goes beyond the available memory. I store it in a file, allocating a certain number of bytes for each element. Everything suits, but takes a long time to record. I want to do something like a temporary buffer, and write elements of 100, for example.

  • $ array1 = array_fill (0, count ($ array) * 3.0); - rjhdby
  • And what happens if the item does not climb in 3 bytes? - rjhdby
  • The remaining bytes are simply not written. Those. if it was qwerty , it will be - qwe - A1essandro
  • You can create a long string and work with it as with an array, byte-by-byte. At the end of the work, reset the entire line to the file - Mike
  • Describe the entire task. Most likely, it will be faster (but not easier!) To do AOF + compacting. Well, nobody canceled the block record, you don’t need to change one record each time, you can immediately block it. - etki

1 answer 1

Then so

 $out=array(); for($i=0;$i<count($array);$i++) { $temp=str_split($array[$i]); $out[]=$temp[0]; $out[]=isset($temp[1])?$temp[1]:chr(0); $out[]=isset($temp[2])?$temp[2]:chr(0); } 

And then $ out to file

Well, or instead of an array, you can concatenate a string

  • Hmm, I expected your method to be either the same or less fast. But it is about 2-3 times faster than mine. - A1essandro