php - How to encode to JSON encoded array from non sequential array -


Using CakePHP (on PHP 5.2.6) I query a list of tables that gives an array such as:

array (

  [0] = & gt; not [3] => value 1 [5] => value2  < / Pre> 

)

Since json_encode () does not encode an object instead of a JSR array, since it is not a sequential array, I need to fill the zodiacal selection.

PHP 5.2.6 does not support any additional parameters, so I can not force it to create a JSON encoded array.

My question, does anyone know how I can solve this problem in a clean way?

see:

array_values ​​() returns all returns and input array Array (0 = & gt; None ', 3 = & gt;' value 1 ', numerically

for example

', 5 = & gt;' value2 '); $ X = array_values ​​($ a); Print_r ($ x); Echo json_encode ($ x); Array ([0] = & gt; none [1] => 1 value [2] => value 2) ["

Edit: There may not be a difference in JavaScript arrays. You must fill the missing elements with the example zero.

  $ a = array (0 = & gt; 'none', 3 = & gt; 'value1', 5 = & gt; 'value2'); $ Na = array_pad (array () (), max (array_keys ($ a)), tap); $ A + = $ na; Ksort (one $); Echo json_encode ($ a);  

print ["none", empty, empty, "value1", zero, "value2"]


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -