1

I'm having some trouble figuring out how to take JSON results from a MySQL query; turn them into a PHP array; remove the identical fields from that array, and turn the array back into JSON. The [1] is the part of the row with the actual JSON in it.

Am I missing something? Having trouble finding any similar questions on the site. Thanks!

$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",}]}

Here's the MySQL query I'm using:

"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;";
11
  • 1
    What does not work? Do you have several level in your array? Commented Jul 1, 2013 at 22:00
  • What does "remove the identical fields" mean? The question is meaningless without knowing what your JSON looks like. Commented Jul 1, 2013 at 22:00
  • I don't see any errors in logic. Show us the results of print_r Commented Jul 1, 2013 at 22:00
  • @Savageman: couldn't be multilevel, since it's coming from a mysql_fetch_row. it'll be an ordinary 1-d array Commented Jul 1, 2013 at 22:01
  • 1
    So what problems are you actually having? What results do you expect and what do you get? Are you getting errors? Commented Jul 1, 2013 at 22:03

1 Answer 1

1

It seems that you are trying to access array elements in a JSON encoded string ($data[1]).
I had success with the following code:

$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

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

Comments

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.