1

I have the following problem I can not achieve the desired format of an array. I have:

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

and I produce this array:

{"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]}

and I need this format:

[{"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]}]

This is my Query

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

Thank you all for the help

asked Sep 12, 2011 at 14:20

1 Answer 1

1

Something like this should get you there.

while ($row = mysql_fetch_assoc ($result))
{
 $alias = $row['Alias​​'];
 $read = (int) $row['Read'];
 $data[] = array
 (
 'name' => $alias,
 'data' => $read,
 );
}
answered Sep 12, 2011 at 14:24
Sign up to request clarification or add additional context in comments.

3 Comments

NOP, The result is: [{"name":"310-Con","data":-74},{"name":"310-Con","data":-74},{"name":"310-Con","data":-72},{"name":"310-Con","data":-73},{"name":"310-Nev3","data":5},{"name":"310-Nev3","data":5},{"name":"310-Nev3","data":4},{"name":"310-Nev3","data":5},{"name":"310-Nev3","data":5},{"name":"310-Nev3","data":5}]
Your query isn't what I expected it to be then :) Can you post your SQL?
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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.