Location: PHPKode > scripts > Variable Length Coding > variable-length-coding/example.php
<?php

require './vlc.class.php';

/* Example usage */

// create a sorted array with 1,000 random values
$values = array();
for( $i=0; $i<1000; $i++ ) {
	$values[] = rand(1,999999);
}
sort($values);

print_r( $values );

$vlcw = new VLCWriter();

// store the first value as is
$base = array_shift( $values );
$vlcw->zipInt( $base );

// for all other values, store the delta
while( $value = array_shift( $values ) ) {
	$delta = $value - $base;
	$vlcw->zipInt( $delta );
	$base = $value;
}

// get the binary output
$data = $vlcw->toString();

// normal length is 1000*4 bytes (1000 32-bit integers)
$encoded_length = strlen( $data );
$gain = ( 1000 * 4 ) - $encoded_length;

print "Original length: 4000 bytes\n";
print "Encoded length: " . $encoded_length . " bytes\n";

// Decode the binary data

$vlcr = new VLCReader( $data );

$values = array();
$base = 0;

while( $delta = $vlcr->unzipInt() ) {
	$value = $base + $delta;
	$values[] = $value;
	$base = $value;
}

print_r( $values );

?>
Return current item: Variable Length Coding