数组从mysql结果多维

时间:2011-09-12 14:20:37

标签: php mysql arrays multidimensional-array

我有以下问题我无法实现所需的数组格式。 我有:

while ($ row = mysql_fetch_assoc ($ result))
{
$ alias = $ row ['Alias​​'];
$ read = (int) $ row ['Read'];
$ data [$ alias] [] = $ read;
}

我制作了这个数组:

{"310-Amb":[23,24,24,25],
"310-Nev1":[5,5],
"310-Nev2":[6,6],
"310-Nev3":[5,5,4,4,5,4,5,5,5,4,4,5,5]}

我需要这种格式:

[{"name":"310-Amb","data":[23,24,24,25]},
{"name":"310-Nev1","data":[-74]},
{"name":"310-Nev2","data":[5]},
{"name":"310-Nev3","data":[5,6,6,5,5,4,4,5,4,5,5,5]}]

这是我的查询

SELECT read.Id, read.Fecha, read.Hora, productos.Producto, neveras.Alias, neveras.Min,
neveras.Max, read.Lectura FROM read Inner Join neveras ON read.Nevera_Id = neveras.Id 
Inner Join productos ON neveras.Producto = productos.Id WHERE Hora between
SUBTIME(CURTIME() , '03:00:00') And CURTIME() And read.$TipoClienteX = $IdX 
ORDER BY Alias, Hora Asc

谢谢大家的帮助

1 个答案:

答案 0 :(得分:1)

这样的事可以让你到那儿。

while ($row = mysql_fetch_assoc ($result))
{
    $alias = $row['Alias​​'];
    $read = (int) $row['Read'];
    $data[] = array
    (
        'name' => $alias,
        'data' => $read,
    );
}