将JSON解码为PHP数组,删除相同的条目并编码回PHP


Decoding JSON into a PHP array, removing identical entries and encoding back to PHP

我在弄清楚如何从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;";

您似乎正在尝试访问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