如果我的json_encode输出如此。
[{"id":"nameid","src":"http#"},{"id":"nameid","src":"http#"}]
我怎么能变成像:
[["name1","address1"],["name2","address2"]]
答案 0 :(得分:1)
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]);
}
(这是由coffeescript生成的。咖啡因来源FYI要小得多)
source = [{"id":"nameid","src":"http#"},{"id":"nameid","src":"http#"}]
result = []
result.push([obj.id, obj.src]) for obj in source
答案 1 :(得分:0)
假设您的意思是name1
是第一个对象的值nameid
,等等......
在将其传递给json_encode()
之前将其转换为所需的数组格式。这样的事可能适合你:
// 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);