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) {
$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?
2 Answers 2
$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.
answered Sep 27, 2011 at 1:54
Niet the Dark Absol
326k86 gold badges480 silver badges604 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
user660943
This is exactly what I was hoping to accomplish. Thank you very much.
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
answered Sep 27, 2011 at 1:48
Ignacio Vazquez-Abrams
804k160 gold badges1.4k silver badges1.4k bronze badges
1 Comment
Ignacio Vazquez-Abrams
Yes, hence the "js>".
print() is specific to the REPL though.lang-php