你好我想获得那种数据库格式:
2013-10-10, 4, username1
2013-10-10, 20, username2
2013-10-12, 1, username1
到一个看起来像这样的数组:
Array(
[0] =>
Array(
[timestamp] => 2013-10-10,
[username1] => 4, *from username1*
[username2] => 20 *from username2*
[1] =>
Array(
[timestamp] => 2013-10-12,
[username1] => 1
)
)
我试过了:
function generatedata(data)
{
var datas = [];
for (item in data)
{
// console.log(data[item]['qty']); Logs well as numbers
var dater = data[item]['timestamp'];
if (!datas.hasOwnProperty(dater))
{
datas[dater] = [];
var date = new Date(data[item]['timestamp'].replace(' ', 'T'));
datas[dater]['date'] = date;
}
datas[dater][data[item]['name']] += data[item]['qty']; // gets assined as undefinedXX like undefined22
}
return datas;
}
由名称确定的值为'undefined22'或'undefined1',即使我检查其是否存在损失 我使用PHP和JavaScript 甚至没有想法
答案 0 :(得分:0)
试试这个:
var timestamp_to_id = {};
var last_id = 0;
var out = [];
for(var i = 0, data; data = datas[i++];)
{
var timestamp = new Date(data[0].replace(' ', 'T'));
var qty = data[1];
var username = data[2];
var id = timestamp_to_id[timestamp];
if (id === undefined) {
id = timestamp_to_id[timestamp] = last_id ++;
out[id] = { timestamp: timestamp };
}
out[id][username] = qty;
}
请参阅控制台(F12)