php json encode problem
In order to access this data via ajax, how can I give each json encoded value a constant name?
$data = array();
foreach($query->result() as $row) {
开发者_JAVA百科 $data[$row->id] = $row->name;
}
This returns json in this format:
{"12428":"Alpine 12\" Single-Voice-Coil 4-Ohm Subwoofer",}
The id (12428) is not constant and therefore, I have nothing constant to look for when trying to decode the data with ajax.
How can I change the php code to add a constant value foreach of the encoded json items?
$data = Array();
foreach($query->result() as $row) {
$data[] = Array("id" => $row->id, "name" => $row->name);
}
Then, your JSON object looks like:
[{"id":"12428","name","Alpine 12\" Single-Voice-Coil 4-Ohm Subwoofer"}]
You can now loop through that array and get the id
and name
of each element.
Who cares.
js> d = {"12428":"Alpine 12\" Single-Voice-Coil 4-Ohm Subwoofer"}
[object Object]
js> for (i in d)
{
print(d[i]);
}
Alpine 12" Single-Voice-Coil 4-Ohm Subwoofer
精彩评论