Eli
Eli

Reputation: 1276

How to convert json encoded data to array in codeigniter?

I have a json encoded data stored in my database. I want to convert this json encoded data to array for specific purpose. I have used json_decode function but it seems not working because I got errors. How can I get back to array structure the data I have saved in my database in json format? Please help. Here is my code. Thanks a lot.

Controller function

function show_at_report(){

      $data = $this->data;
      $report_id = $this->uri->segment(3);
      $at_id = $this->uri->segment(4);
      $query = $this->core_model->get_report_details($report_id,$at_id);
      $a = $query['rows'];
      var_dump($a);


      foreach($a as $b){
       var_dump($b);
      }

    }

Output:

enter image description here

Modified Controller function

function show_at_report(){

      $data = $this->data;
      $report_id = $this->uri->segment(3);
      $at_id = $this->uri->segment(4);
      $query = $this->core_model->get_report_details($report_id,$at_id);
      $a = $query['rows'];
      json_decode($a,TRUE);
      var_dump($a);


      foreach($a as $b){
       json_decode($b,TRUE);
       var_dump($b);
      }

    }

Output showing errors

enter image description here

What is the problem with this? How can I get the json data back to array?

Upvotes: 0

Views: 6618

Answers (1)

Sonaryr
Sonaryr

Reputation: 1122

$b is an array, like the error says, json_decode requires a string. And if I'm seeing correctly the variable you really want to decode is $b[0]->data as $b points to an array with 1 object in it, and that object has a public variable called data, this variable contains your json it seems.

Upvotes: 1

Related Questions