Sanjay Yadav Sanjay Yadav - 5 months ago 76
JSON Question

wordpress json_decode is not working, I am trying to get value form wp_option table

When I am trying to decode JSON with this code the output is

ArrayArray ( [0] => )


I do not know why WordPress doesn't support this

<?php
global $wpdb;
$mylink = $wpdb->get_results( "SELECT option_value FROM wp_options WHERE option_id=62167", ARRAY_N );
$raw = stripslashes_deep($mylink);
$data = array();
foreach ($raw as $json) {
echo $json;

$item = @json_decode($json, true);

$data[] = $item;

print_r($data);

}
?>

Answer

Hello you need to unserealize the data first when your query get data from database, That is serealized data and when we get it from db we need to unserealize it.

 global $wpdb;
    $mylink = $wpdb->get_results("SELECT option_value FROM wp_options WHERE option_id=1223",ARRAY_A);

   $raw = stripslashes_deep($mylink);
  $data = $raw[0]['option_value'];
  $datas = unserialize($data);
  foreach ($datas as $key => $value) {
    print_r($value);
  }