PHP Classes

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

Recommend this page to a friend!

  Detailed description   Download Download .zip .tar.gz  
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.

  Author Author  
Picture of Ramesh Narayan Jangid
Name: Ramesh Narayan Jangid <contact>
Classes: 6 packages by
Country: India India
Age: 47
All time rank: 3445227 in India India
Week rank: 21 Up3 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;

  Classes of Ramesh Narayan Jangid  >  PHP JSON Encode Large Data  >  Download Download .zip .tar.gz  >  Support forum Support forum  >  Blog Blog (1)  
Name: PHP JSON Encode Large Data
Base name: heavyjsonencode
Description: Encode large arrays to JSON using less memory
Version: -
PHP version: 5
License: MIT/X Consortium License
All time users: 188 users
All time rank: 8587
Week users: 2 users
Week rank: 67 Up
 
  Groups   Rate classes User ratings   Applications   Files Files  

  Groups  
Group folder image PHP 5 Classes using PHP 5 specific features View top rated classes
Group folder image Data types Modeling and manipulating data types View top rated classes


  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

  User ratings  
Not enough user ratings

  Applications that use this package  
No pages of applications that use this class were specified.

Add link image If you know an application of this package, send a message to the author to add a link here.

  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

Download Download all files: heavyjsonencode.tar.gz heavyjsonencode.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.
  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

Download Download all files: heavyjsonencode.tar.gz heavyjsonencode.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.