PHP Classes

How to Use PHP json_encode to Store a Large Number of Arrays or Objects Using the Package Heavy Objects: Store and retrieve objects or arrays in files

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-02-19 (Yesterday) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
heavy-objects 1.0MIT/X Consortium ...7Tools, Files and Folders, Performance..., P...
Description 

Author

This package can store and retrieve objects or arrays in files.

It provides classes that open files and read or write values objects or arrays.

The classes use JSON as the format to store a serialized version of the arrays or objects.

Picture of Ramesh Narayan Jangid(Sharma)
  Performance   Level  
Name: Ramesh Narayan ... <contact>
Classes: 11 packages by
Country: India India
Innovation award
Innovation award
Nominee: 5x

Winner: 4x

Example

<?php
use HeavyObjects\Source\HeavyObject;

include_once(
'autoload.php');

$stream = fopen("php://temp", "rw+b");
$heavyObject = new HeavyObject($stream);

// Execute DB Query
$stmt = $db->select($sql);

// Load/Write/Update records to file
for ($i=0; $row = $stmt->fetch(PDO::FETCH_ASSOC); $i++) {
   
$heavyObject->write($row, $keys = "row:{$i}");
}

// Get/Read records from file
$key = 10;
$row = $heavyObject->read("row:{$key}");
echo
'<pre>';
echo
'row:'; print_r($row);
echo
'Count:' . $heavyObject->count('row');


Details

Heavy-Objects

Manage Heavy(RAM intensive) Array/Object Collections via single File in filesystem using limited RAM.

Examples

Memory usage by 1000 raw objects each with 100 keys (~12 MB)

<?php
$rows = [];

for ($i=0; $i<1000; $i++) {
    $row = [];
    for($j=0;$j<100;$j++) {
        $row["Key{$j}"] = rand();
    }
    $rows[] = $row;
}

echo '<pre>';
echo memory_get_usage(); // 11,842,144 bytes

Memory usage by 1000 HeavyObjects each with 100 keys (~2.5 MB)

<?php
use HeavyObjects\Source\HeavyObject;

include_once('autoload.php');

$stream = fopen("php://temp", "rw+b");
$heavyObject = new HeavyObject($stream);

// Load/Write/Update records to file
for ($i=0; $i<1000; $i++) {
    $row = [];
    for($j=0;$j<100;$j++) {
        $row["Key{$j}"] = rand();
    }
    $heavyObject->write($row, $keys = "row:{$i}");
}

echo '<pre>';
echo memory_get_usage(); // 2,659,224 bytes

  Files folder image Files (6)  
File Role Description
Files folder imageSource (2 files)
Plain text file autoload.php Class Class source
Accessible without login Plain text file Example.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Read me to learn from an documented example

  Files folder image Files (6)  /  Source  
File Role Description
  Plain text file Engine.php Class Class source
  Plain text file HeavyObject.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0