PHP Classes

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

Recommend this page to a friend!
  Info   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Last Updated Ratings Unique User Downloads Download Rankings
2023-09-02 (3 months ago) RSS 2.0 feedNot enough user ratingsTotal: 192 All time: 8,533 This week: 358Up
Version License PHP version Categories
heavyjsonencode 1.0MIT/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: 6 packages by
Country: India India
Age: 48
All time rank: 3424227 in India India
Week rank: 189 Up13 in India India Up
Innovation award
Innovation award
Nominee: 2x

Winner: 1x

Details

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.

Example:

<?php
$sth = $db->select($sql);
$sth->execute($params);

$jsonEncode = new JsonEncode();

// For a single row

$jsonEncode->startAssoc();
foreach($sth->fetch(PDO::FETCH_ASSOC) as $key => $value) {
    $jsonEncode->addKeyValue($key, $value);
}
$jsonEncode->endAssoc();
// OR
$jsonEncode->encode($sth->fetch(PDO::FETCH_ASSOC));

// For multiple rows

$jsonEncode->startArray();
for(;$row=$sth->fetch(PDO::FETCH_ASSOC);) {
    $jsonEncode->encode($row);
}
$jsonEncode->endArray();

// For output of both assoc and simple array.
// Array inside Associative array.

$jsonEncode->startAssoc();
foreach($sth->fetch(PDO::FETCH_ASSOC) as $key => $value) {
    $jsonEncode->addKeyValue($key, $value);
}

$sth_2 = $db->select($sql_2);
$sth_2->execute($params_2);

$jsonEncode->startArray($assocKey);
for(;$row=$sth_2->fetch(PDO::FETCH_ASSOC);) {
    $jsonEncode->encode($row);
}
$jsonEncode->endArray();
$jsonEncode->endAssoc();

$jsonEncode = null;

  Files folder image Files  
File Role Description
Plain text file JsonEncode.php Class Encode Array to JSON
Accessible without login Plain text file Readme.md Doc. Readme

 Version Control Unique User Downloads Download Rankings  
 0%
Total:192
This week:0
All time:8,533
This week:358Up