PHP Classes

PHP JSON Encode Large Data: Encode large arrays to JSON using less memory

Recommend this page to a friend!
  Info   View files Example   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2024-07-05 (Yesterday) RSS 2.0 feedNot enough user ratingsTotal: 204 All time: 8,437 This week: 52Up
Version License PHP version Categories
heavyjsonencode 2.0.3MIT/X Consortium ...5PHP 5, Data types
Description 

Author

This package can encode large arrays to JSON using less memory.

It can output JSON-encoded string for an array by adding each array element immediately.

The JSON-encoded string is outputted as part of the current PHP script output without storing the whole array of elements as a variable, thus avoiding taking too much memory.

Innovation Award
PHP Programming Innovation award nominee
April 2023
Number 11
JSON is a format for encoding data structures as strings. PHP can encode any variable type using the JSON format.

Suppose you want to encode an array with many elements. In that case, the PHP JSON encoding function can take a lot of memory because it needs to take a variable as a parameter with all the elements and create a string in memory with the result of the JSON encoding process.

This package provides a better solution that takes much less memory. It can encode one array element at a time and outputs it immediately without adding all elements to the array.

This way, the class does not store all array elements, and that will save a lot of memory, keeping the memory usage within limits set in the PHP configuration.



Manuel Lemos
Picture of Ramesh Narayan Jangid
  Performance   Level  
Name: Ramesh Narayan Jangid <contact>
Classes: 8 packages by
Country: India India
Innovation award
Innovation award
Nominee: 3x

Winner: 2x

Example

JSON Encode

PHP JSON Encode large data with lesser resources

Examples

Generating single row output as object.

<?php
require "JsonEncode.php";

// Create JsonEncode Object.
$jsonEncodeObj = JsonEncoder::getObject();

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

// For single row - one
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$jsonEncode->encode($row);

// For single row - two
$jsonEncode->startObject();
foreach($stmt->fetch(PDO::FETCH_ASSOC) as $key => $value) {
    $jsonEncode->addKeyValue($key, $value);
}
$jsonEncode->endObject();

// Free statement resources and close the cursor.
$stmt->closeCursor();

$jsonEncode = null;

Generating many rows output as array of objects.

<?php
require "JsonEncode.php";

// Create JsonEncode Object.
$jsonEncodeObj = JsonEncoder::getObject();

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

// For multiple rows
$jsonEncode->startArray();
for(;$row=$stmt->fetch(PDO::FETCH_ASSOC);) {
    $jsonEncode->encode($row);
}
$jsonEncode->endArray();

// Free statement resources and close the cursor.
$stmt->closeCursor();

$jsonEncode = null;

Generating single row output inside object.

<?php
require "JsonEncode.php";

// Create JsonEncode Object.
$jsonEncodeObj = JsonEncoder::getObject();

// Start JSON object
$jsonEncode->startObject();

// Execute DB Query - 1
$stmt = $db->select($sql);
$stmt->execute($params);
foreach($stmt->fetch(PDO::FETCH_ASSOC) as $key => $value) {
    $jsonEncode->addKeyValue($key, $value);
}
// Free statement resources and close the cursor.
$stmt->closeCursor();

// Execute DB Query - 2 (which returns single row)
$stmt = $db->select($sql_2);
$stmt->execute($params_2);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor();

// Append key / value pair (value can be an integer / string / array)
$jsonEncode->addKeyValue('subCatgories', $row);

// End JSON object
$jsonEncode->endObject();

$jsonEncode = null;

Generating many rows output inside object.

<?php
require "JsonEncode.php";

// Create JsonEncode Object.
$jsonEncodeObj = JsonEncoder::getObject();

// Start JSON object
$jsonEncode->startObject();

// Execute DB Query - 1
$stmt = $db->select($sql);
$stmt->execute($params);
foreach($stmt->fetch(PDO::FETCH_ASSOC) as $key => $value) {
    $jsonEncode->addKeyValue($key, $value);
}
// Free statement resources and close the cursor.
$stmt->closeCursor();

// Start JSON array inside object
$objectKey = 'subCatgories';
$jsonEncode->startArray($objectKey);

// Execute DB Query - 2
$stmt = $db->select($sql_2);
$stmt->execute($params_2);
for(; $row=$stmt->fetch(PDO::FETCH_ASSOC);) {
    $jsonEncode->encode($row);
}
// Free statement resources and close the cursor.
$stmt->closeCursor();

// End JSON array inside object
$jsonEncode->endArray();

// End JSON object
$jsonEncode->endObject();

$jsonEncode = null;

> The $jsonEncode = null; will stream the generated JSON.


  Files folder image Files  
File Role Description
Accessible without login Plain text file composer.json Data Auxiliary data
Plain text file JsonEncode.php Class Encode Array to JSON
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Example Example script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:204
This week:0
All time:8,437
This week:52Up