Advertisement
Alhadis

Compress JSON Array in PHP

Jun 23rd, 2015
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.34 KB | None | 0 0
  1. /**
  2.  * Converts a list of associative arrays into indexed arrays.
  3.  *
  4.  * Each property name is collated within an extra array inserted at the beginning
  5.  * of the returned array, structuring the data in a CSV-like fashion. For example:
  6.  *
  7.  *   array(
  8.  *       array('first_name' => 'John',   'last_name' => 'Gardner',   'age' => 28),
  9.  *       array('first_name' => 'Sum',    'last_name' => 'Kunt',      'age' => 25)
  10.  *   )
  11.  *
  12.  * Would be returned as:
  13.  *
  14.  *   array(
  15.  *       array('first_name', 'last_name',    'age'),
  16.  *       array('John',       'Gardner',      28),
  17.  *       array('Sum',        'Kunt',         25)
  18.  *   )
  19.  *
  20.  * Properties are indexed in the order they're encountered when traversing the original data
  21.  * list, with any missing properties assigned empty values if they're present in some rows
  22.  * but absent in others. For instance, consider the following inconsistent data structure,
  23.  * with two rows sharing only partial similarity:
  24.  *
  25.  *   array(
  26.  *       array('first_name' => 'John',   'age' =>  28),
  27.  *       array('first_name' => 'Sum',    'dob' => '1990-08-20')
  28.  *   )
  29.  *
  30.  * These would be collated and returned as:
  31.  *
  32.  *   array(
  33.  *       array('first_name',   'age',    'dob'),
  34.  *       array('John',         '28',      NULL),
  35.  *       array('Sum',           NULL,    '1990-08-20')
  36.  *   )
  37.  *
  38.  * This ensures data will be ordered in a consistent format, and can help reduce latency
  39.  * and bandwidth costs when sending large payloads of JSON-encoded data.
  40.  *
  41.  * @param array $data - A multidimensional array to operate upon
  42.  * @return array
  43.  */
  44. function collate_arrays($data){
  45.     $output     =   array();
  46.  
  47.     # First, cycle through each row to extract a list of every unique field name that's present.
  48.     $fields     =   array();
  49.     $count      =   0;
  50.  
  51.     foreach($data as $row){
  52.         foreach($row as $name => $value)
  53.             if(!isset($fields[$name]))
  54.                 $fields[$name]  =   $count++;
  55.     }
  56.  
  57.  
  58.     # Flip the list of field names and store them in the first row of the returned array.
  59.     $fields     =   array_flip($fields);
  60.     $output[]   =   $fields;
  61.  
  62.  
  63.     # Next, run through the array again, ensuring every array and its properties are consistently indexed in running field order.
  64.     foreach($data as $row){
  65.         $indexed_row = array();
  66.  
  67.         for($i = 0; $i < $count; ++$i)
  68.             $indexed_row[$i] = $row[$fields[$i]];
  69.  
  70.         $output[]   =   $indexed_row;
  71.     }
  72.  
  73.     return $output;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement