1

I have a PHP code which has the following output:

[{"stockdata":29655.88482666}]

And I need to change the code on my php to get the result in this format:

[{"data": [29655.88482666]}]

This is the code of my php:

<?php


function getArraySQL(){
$startd = "29964";
$endd = "29968";
$dsn = "prueba";
$connect = odbc_connect( $dsn, '', '' );
$query = "

Select SUM(TON_DESCARGADO) as stockdata
    from 
        (Select unit,[load],enum_LOAD.[name],SUM(dumptons) as TON_DESCARGADO
        from hist_dumps inner join hist_loclist on hist_dumps.shiftindex = hist_loclist.shiftindex
                                       and hist_dumps.loc = hist_loclist.locid
                inner join enum_LOAD on hist_dumps.[load] = enum_LOAD.[num]
        where hist_dumps.shiftindex between '$startd' and  '$endd'

GROUP BY loc,UNIT,unit#,[load],enum_LOAD.[name])TEMP1
where unit = 'Stockpile'
GROUP BY unit
order BY UNIT";


if(!$rs = odbc_exec($connect, $query)) die();

$rawdata = array();

$i=0;

while($row = odbc_fetch_array($rs))
{   
$rawdata[$i] = $row;
$i++;
}
odbc_close( $connect );
return $rawdata;    
};
$stockdata = getArraySQL();
echo json_encode(($stockdata), JSON_NUMERIC_CHECK);

What need to be changed to get the correct format? Should I do something to improve the code of my php?

How should I write it if I connect php to SQL server via PDO and get the same result? (just an optional doubt)

6
  • Could you var_dump your $rawdata please? Commented Aug 9, 2015 at 5:15
  • 1
    You haven't stated why you want to change it in that format, because {data: [<some_value>]} means your data key represents an array. Commented Aug 9, 2015 at 5:16
  • because of this: stackoverflow.com/questions/31900565/… Commented Aug 9, 2015 at 5:16
  • array(0) { } [[{"stockdata":29655.88482666}]] Commented Aug 9, 2015 at 5:21
  • I need to put the data on a highchart gauge, and doesn't accept the output generated by php Commented Aug 9, 2015 at 5:22

1 Answer 1

1

I don't find custom output in json_encode, so if you want to customize the output of array , you would need to prepare it. I assumed the data exists at zero index (0).
So, I use $stockdata[0]
for JSON_NUMERIC_CHECK replace, used (float)

here is the preparation of output to send exactly you want.

$stockdata = getArraySQL();
$Numeric_data = (float) $stockdata[0];
$data = '[{"data": ['.$Numeric_data.']}]';
echo $data;

Demo

Sign up to request clarification or add additional context in comments.

3 Comments

add echo '<pre>';print_r($stockdata);echo '</pre>'; right after the $stockdata = getArraySQL();
Array ( [0] => Array ( [data] => 29655.884826660156 ) ) [{"data": [1]}]
i think that the problem isn't of he php. Probably the Highchart require only the numeric value from the json.... I will see how to do that

Your Answer

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.