BMLT Root Server
Array2Json.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************************/
3 /** \file Array2JSON.php
4 
5  \brief This is an open-source JSON encoder that allows us to support
6  older versions of PHP (before the <a href="http://us3.php.net/json_encode">json_encode()</a> function
7  was implemented). It uses json_encode() if that function is available.
8 
9  This is from <a href="http://www.bin-co.com/php/scripts/array2json/">Bin-Co.com</a>.
10 
11  This crap needs to be included to be aboveboard and legal. You can still re-use the code, but
12  you need to make sure that the comments below this are included:
13 
14  Copyright (c) 2004-2007, Binny V Abraham
15 
16  All rights reserved.
17 
18  Redistribution and use in source and binary forms, with or without modification, are permitted provided
19  that the following conditions are met:
20 
21  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
22  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
23  in the documentation and/or other materials provided with the distribution.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
26  BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 defined('BMLT_EXEC') or die('Cannot Execute Directly'); // Makes sure that this file is in the correct context.
35 
36 /*******************************************************************/
37 /** \brief Encodes a given associative array into a JSON object string.
38 
39  \returns a JSON object, as a string.
40 */
41 function array2json(
42  $arr ///< An associative string, to be encoded as JSON.
43 ) {
44  $parts = array();
45  // See if we are an associative array.
46  $is_list = (count(array_filter(array_keys($arr), 'is_numeric')) == count($arr)) && (array_keys($arr) === range(0, count($arr) - 1));
47 
48  foreach ($arr as $key => $value) {
49  if (is_array($value) && count($value)) { //Custom handling for arrays
50  if ($is_list) {
51  $parts[] = array2json($value); /* :RECURSION: */
52  } else {
53  $parts[] = '"' . $key . '":' . array2json($value); /* :RECURSION: */
54  }
55  } else {
56  $str = '';
57  if ($key === 'json_data') { // JSON data is passed through without any vetting or modification (so it can easily bork the stream).
58  $str = '"'.$key.'":'.$value;
59  } else {
60  if (!$is_list) {
61  $str = '"' . $key . '":';
62  }
63 
64  //Custom handling for multiple data types
65  if (isset($value)) {
66  if (is_integer($value)) {
67  $str .= $value;
68  } elseif (is_bool($value)) {
69  $str .= $value ? 'true': 'false';
70  } else {
71  $str .= '"' . trim(json_encode(str_replace('"', '&quot;', $value), JSON_UNESCAPED_SLASHES), '"') . '"'; //All other things
72  }
73  } else {
74  $str .= '""'; //All other things
75  }
76  }
77 
78  // :TODO: Is there any more datatype we should be in the lookout for? (Object?)
79 
80  $parts[] = $str;
81  }
82  }
83 
84  $json = implode(',', $parts);
85 
86  if ($is_list) {
87  return '[' . $json . ']'; //Return numerical JSON
88  } else {
89  return '{' . $json . '}'; //Return associative JSON
90  }
91 }
array2json($arr)
Encodes a given associative array into a JSON object string.
Definition: Array2Json.php:41
defined('BMLT_EXEC') or define('BMLT_EXEC'
Definition: index.php:3