PHP json_encode array for Ext JS 4 with success : true

{"success":true, "years":["yearnumber":2012,"yearnumber":2013,...]}

is not valid JSON. Arrays ([]) can’t have keys in them, only values. The best solution (in this scenario) is to just cut they keys since they’re all the same anyway (See Esailija’s answer)

Another way would be to create an array of objects like this

{"success":true, "years":[{"yearnumber":2012},{"yearnumber":2013},...]}

To achieve that from PHP:

$rt = array(); $rt["success"] = true; $rt["years"] = array(); for ($i=intval(date('Y')) ; $i < (date('Y')+21) ; $i++) { $rt['years'][] = array('yearnumber' => $i); } echo json_encode($rt);

Another sample from us

* Created by JetBrains PhpStorm.
* User: purnama
* Date: 4/8/13
* Time: 11:19 AM
* To change this template use File | Settings | File Templates.

$rsdeptsql = "select * from tbdept";
$rsdeptquery = mysql_query($rsdeptsql);
$rsdeptrow = mysql_fetch_assoc($rsdeptquery);

while ($rsdeptrow = mysql_fetch_assoc($rsdeptquery))

$data[Departments]= array($rsdeptrow);

echo json_encode($data);

SISINDOTEK – IT Training & Solution Provider
Jl. Sukasenang VI-6B Bandung – Jawa Barat 40124
Tel. 022-71242266 , SMS. 0812.8733.1966
info , YM. sisindotek , ,

Please consider your environmental responsibility.
Before printing this e-mail message, ask yourself whether you really need a hard copy.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s