1

我在弄清楚如何从 MySQL 查询中获取 JSON 结果时遇到了一些麻烦;把它们变成一个 PHP 数组;从该数组中删除相同的字段,然后将数组转回 JSON。[1] 是包含实际 JSON 的行的一部分。

我错过了什么吗?无法在网站上找到任何类似的问题。谢谢!

$data = mysql_fetch_row($result);
print_r($data);

$json = json_decode($data[1], TRUE);
var_dump($json);
print_r($json);

$distinctresult = array_unique($json);
print_r($distinctresult);

$final = json_encode($distinctresult);


{"rows":[{"level":"ERROR","key":"Standard Not found","value":"RI.1.8"},{"level":"ERROR","key":"Standard Not found",{"level":"ERROR","key":"Standard Not found","value":"RI.K.9"},{"level":"ERROR","key":"Standard Not found","value":"RI.K.9"},{"level":"ERROR","key":"Standard Not found","value":"RI.K.9",}]}

这是我正在使用的 MySQL 查询:

"select distinct d.valueField
    from etllogs t
    inner join etllogdetails d on t.uid = d.etllogID and d.valueField like '%ERROR%'
    where t.transformationName like 'CM Data Extract'
    and (t.timestamp >= (now() - interval 24 hour))
    order by t.timestamp desc;";
4

1 回答 1

1

您似乎正在尝试访问 JSON 编码字符串 ( $data[1]) 中的数组元素。
我使用以下代码取得了成功:

$data = array(0=>array('column1'=>'value1','column2'=>'value2'),
              1=>array('column3'=>'value3','column4'=>'value3'));

$data_json=json_encode($data);
echo"ORIGINAL JSON:<pre>".print_r($data_json,true)."</pre>";

$data_php=json_decode($data_json,true);
echo"PHP ARRAY:<pre>".print_r($data_php,true)."</pre>";

$data_chunk=$data_php[1];
echo"PHP ARRAY CHUNK:<pre>".print_r($data_chunk,true)."</pre>";

$distinctresult = array_unique($data_chunk);
echo"UNIQUE CHUNK:<pre>".print_r($distinctresult,true)."</pre>";

$final = json_encode($distinctresult);
echo"FINAL JSON:<pre>".print_r($final,true)."</pre>";

http://phpfiddle.org/main/code/7dg-nnb

于 2013-07-01T22:19:30.270 回答