if my json_encode outputs like this.
[{"id":"nameid","src":"http#"},{"id":"nameid","src":"http#"}]
how can i turn into something like:
[["name1","address1"],["name2","address2"]]
if my json_encode outputs like this.
[{"id":"nameid","src":"http#"},{"id":"nameid","src":"http#"}]
how can i turn into something like:
[["name1","address1"],["name2","address2"]]
var obj, result, source, _i, _len;
source = [
{
"id": "nameid",
"src": "http#"
}, {
"id": "nameid",
"src": "http#"
}
];
result = [];
for (_i = 0, _len = source.length; _i < _len; _i++) {
obj = source[_i];
result.push([obj.id, obj.src]);
}
(That was generated by coffeescript. The coffeescript source FYI is much smaller)
source = [{"id":"nameid","src":"http#"},{"id":"nameid","src":"http#"}]
result = []
result.push([obj.id, obj.src]) for obj in source
Assuming you mean that name1 is the value nameid from the first object, etc...
Convert it to the array format you want before passing it to json_encode(). Something like this might work for you:
// Assuming your objects are an array $objects
$output_array = array();
foreach ($objects as $o) {
// Put the two properties from the object into an array
// and append it to $output_array
$output_array[] = array($o->id, $o->src);
}
// Encode the array as json
$json = json_encode($output_array);