[英]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>";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.