0

I want to extract value from json stored in db, but i cannot figure it out. here's my db looks like :

+-------------------+-------------------------------------------------+---------------------+---------------------+
| refid             |                      kepribadian                | jam_mulai           | jam_selesai         |
+-------------------+-------------------------------------------------+---------------------+---------------------+
| GAD25D335B0F1DE3B | {"Introversion":"53.3%","Extroversion":"46.7%"} | 2019-08-09 01:16:00 | 2019-08-09 01:19:46 |
| GAD25D32DAE90578A | {"Introversion":"63.3%","Extroversion":"34.7%"} | 2019-08-09 08:55:47 | 2019-08-09 08:56:48 |
+-------------------+-------------------------------------------------+---------------------+---------------------+

And this is my models :

public function filter_hasil_mbti($search, $limit, $start, $order_field, $order_ascdesc)
{

    $sql =  $this->db->select('a.id AS id, a.refid AS refid, CONCAT(b.firstname," ",b.lastname) AS fullname, a.kepribadian AS kepribadian, a.jam_mulai AS jam_mulai, a.jam_selesai AS jam_selesai, a.percentage AS percentage')
            ->from('jawaban_mbti as a')
            ->join('registrasi_baru as b', 'a.refid=b.refid', 'left')
            ->like('a.refid', $search)
            ->or_like('b.firstname', $search)
            ->or_like('b.lastname', $search)
            ->or_like('a.kepribadian', $search)
            ->or_like('a.jam_mulai', $search)
            ->or_like('a.jam_selesai', $search)
            ->order_by($order_field, $order_ascdesc)
            ->limit($limit, $start)
            ->get()->result_array();

    return $sql;
}

So, I can access column kepribadian json as key and store it all in result_array() How can i do that?

Thank you.

UPDATE

Here's my controller :

public function ajax_jawaban_mbti()
{
    $search         = $_POST['search']['value'];
    $limit          = $_POST['length'];
    $start          = $_POST['start'];
    $order_index    = $_POST['order'][0]['column'];
    $order_field    = $_POST['columns'][$order_index]['data'];
    $order_ascdesc  = $_POST['order'][0]['dir'];

    $sql_total      = $this->mhasil->count_all_Soal('jawaban_mbti');
    $sql_data       = $this->mhasil->filter_hasil_mbti($search, $limit, $start, $order_field, $order_ascdesc);
    $sql_filter     = $this->mhasil->count_filter_hasil_mbti($search);

    //print_r($sql_data);die;

    $callback       = array(
        'draw'              => $_POST['draw'],
        'recordsTotal'      => $sql_total,
        'recordsFiltered'   => $sql_filter,
        'data'              => $sql_data,
    );

    //header('Content-Type: application/json');
    echo json_encode($callback);
}
0

3 Answers 3

1

you can use json_decode to convert it to a array like bellow:

Example:

 $json = '{"Introversion":"53.3%","Extroversion":"46.7%"}';
 $array = json_decode($json)
 var_dump($array);

I suggest to change the name of the variable from $sql to $results because you return a array and not the sql

update 1:

foreach ($sql as $key => $val) {
// you can add in the second argument of json_decode 1 to have a array instead of a object.
if(isset($val['kepribadian']))
    $sql[$key]['kepribadian'] = json_decode($val['kepribadian']);
}

the result wil be like this:

    Array
(
    [0] => Array
        (
            [kepribadian] => stdClass Object
                (
                    [Introversion] => 53.3%
                    [Extroversion] => 46.7%
                    [Sense] => 66.7%
                    [Intuition] => 33.3%
                    [Thingking] => 46.7%
                    [Feeling] => 53.3%
                    [Judging] => 66.7%
                    [Perceiving] => 33.3%
                )

        )

    [1] => Array
        (
            [id] => 1337
            [refid] => GAD25D335B0F1DE3B
            [fullname] => Ravhi Rizaldi
            [kepribadian] => 
            [jam_mulai] => 2019-08-09 01:16:00
            [jam_selesai]] => 2019-08-09 01:19:46
        )

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

6 Comments

how to decode it when the data is on the result_array()? it's like array in the array and thanks for your sugestion :)
can you give me a print_r of the result in the description ? the structure
Array ( [0] => Array ( [kepribadian] => {"Introversion":"53.3%","Extroversion":"46.7%","Sense":"66.7%","Intuition":"33.3%","Thingking":"46.7%","Feeling":"53.3%","Judging":"66.7%","Perceiving":"33.3%"} ) [1] => Array ( [id] => 1337 [refid] => GAD25D335B0F1DE3B [fullname] => Ravhi Rizaldi [kepribadian] => [jam_mulai] => 2019-08-09 01:16:00 [jam_selesai] => 2019-08-09 01:19:46 ) )
I got this error : A PHP Error was encountered Severity: Notice Message: Undefined offset: 0
yes i have also updated my reponse update 1, try it
|
0

I guess you will get an array of data from your select statement where the "kepribadian" column is one of the values. So what you have to do is the DECODE this column from JSON to a PHP array.

In principle like this, (not tested)

...
$sqlrec = filter_hasil_mbti(...  ...);
$phpArr = json_decode($sqlrec['kepribadian']);
...

Comments

0

You can try

<?php
    $json_decoded = json_decode(array_column($rsult, "kepribadian"), true);
 ?>

If you add true parameter in json_decode() the result will be given as a PHP array.

Have fun :)

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.