Unserialize data in Codeigniter?

Asked

Viewed 54 times

3

Seeing that I have the following method:

Controller

public function get_objeto($permissao_id)
{
    if ($permissao_id == 0)
    {           
        $permissao_id = $permissao_id;
        $dados = $this->permissao->get($permissao_id);
        echo json_encode($dados, JSON_NUMERIC_CHECK);
    }
    else
    {
        $dados = $this->permissao->get_by_id($permissao_id);

        echo json_encode($dados, JSON_NUMERIC_CHECK);
    }
}

Model

// get_by_id
public function get_by_id($permissao_id)
{       
    $result = array();
    $result = $this->get_data($permissao_id);       
    return $result;
}
// get_data
public function get_data($permissao_id)
{
    $this->db->select('id, nome, permissoes tree_funcao, grupo_id');
    $this->db->from($this->tabela);
    $this->db->where('id',$permissao_id);
    $query = $this->db->get();
    $item = $query->row();      
    return $item;
}

And the following return from the database:

{"id":3,"nome":"teste","tree_funcao":"a:7:{i:0;a:9:{s:2:\"id\";s:1:\"1\";s:4:\"text\";s:9:\"Dashboard\";s:4:\"icon\";s:23:\"fa  fa-folder text-osun\";s:7:\"li_attr\";O:8:\"stdClass\":1:{s:2:\"id\";s:1:\"1\";}s:6:\"a_attr\";O:8:\"stdClass\":2:{s:4:\"href\";s:1:\"#\";s:2:\"id\";s:8:\"1_anchor\";}s:5:\"state\";O:8:\"stdClass\":4:{s:6:\"loaded\";b:1;s:6:\"opened\";b:0;s:8:\"selected\";b:1;s:8:\"disabled\";b:0;}s:4:\"data\";O:8:\"stdClass\":0:{}s:6:\"parent\";s:1:\"#\";s:4:\"type\";s:5:\"grupo\";}i:1;a:9:{s:2:\"id\";s:1:\"2\";s:4:\"text\";s:14:\"Configura\u00e7\u00e3o\";s:4:\"icon\";s:23:\"fa  fa-folder text-osun\";s:7:\"li_attr\";O:8:\"stdClass\":1:{s:2:\"id\";s:1:\"2\";}s:6:\"a_attr\";O:8:\"stdClass\":2:{s:4:\"href\";s:1:\"#\";s:2:\"id\";s:8:\"2_anchor\";}s:5:\"state\";O:8:\"stdClass\":4:{s:6:\"loaded\";b:1;s:6:\"opened\";b:0;s:8:\"selected\";b:1;s:8:\"disabled\";b:0;}s:4:\"data\";O:8:\"stdClass\":0:{}s:6:\"parent\";s:1:\"#\";s:4:\"type\";s:5:\"grupo\";}i:2;a:9:{s:2:\"id\";s:1:\"3\";s:4:\"text\";s:8:\"Usu\u00e1rio\";s:4:\"icon\";s:34:\"fa fa-angle-double-right text-osun\";s:7:\"li_attr\";O:8:\"stdClass\":1:{s:2:\"id\";s:1:\"3\";}s:6:\"a_attr\";O:8:\"stdClass\":2:{s:4:\"href\";s:1:\"#\";s:2:\"id\";s:8:\"3_anchor\";}s:5:\"state\";O:8:\"stdClass\":4:{s:6:\"loaded\";b:1;s:6:\"opened\";b:0;s:8:\"selected\";b:1;s:8:\"disabled\";b:0;}s:4:\"data\";O:8:\"stdClass\":0:{}s:6:\"parent\";s:1:\"2\";s:4:\"type\";s:6:\"funcao\";}i:3;a:9:{s:2:\"id\";s:1:\"4\";s:4:\"text\";s:5:\"Criar\";s:4:\"icon\";s:20:\"fa fa-lock text-osun\";s:7:\"li_attr\";O:8:\"stdClass\":1:{s:2:\"id\";s:1:\"4\";}s:6:\"a_attr\";O:8:\"stdClass\":1:{s:4:\"href\";s:1:\"#\";}s:5:\"state\";O:8:\"stdClass\":4:{s:6:\"loaded\";b:1;s:6:\"opened\";b:0;s:8:\"selected\";b:1;s:8:\"disabled\";b:0;}s:4:\"data\";O:8:\"stdClass\":0:{}s:6:\"parent\";s:1:\"3\";s:4:\"type\";s:4:\"acao\";}i:4;a:9:{s:2:\"id\";s:1:\"5\";s:4:\"text\";s:6:\"Editar\";s:4:\"icon\";s:20:\"fa fa-lock text-osun\";s:7:\"li_attr\";O:8:\"stdClass\":1:{s:2:\"id\";s:1:\"5\";}s:6:\"a_attr\";O:8:\"stdClass\":1:{s:4:\"href\";s:1:\"#\";}s:5:\"state\";O:8:\"stdClass\":4:{s:6:\"loaded\";b:1;s:6:\"opened\";b:0;s:8:\"selected\";b:1;s:8:\"disabled\";b:0;}s:4:\"data\";O:8:\"stdClass\":0:{}s:6:\"parent\";s:1:\"3\";s:4:\"type\";s:4:\"acao\";}i:5;a:9:{s:2:\"id\";s:1:\"6\";s:4:\"text\";s:7:\"Excluir\";s:4:\"icon\";s:20:\"fa fa-lock text-osun\";s:7:\"li_attr\";O:8:\"stdClass\":1:{s:2:\"id\";s:1:\"6\";}s:6:\"a_attr\";O:8:\"stdClass\":1:{s:4:\"href\";s:1:\"#\";}s:5:\"state\";O:8:\"stdClass\":4:{s:6:\"loaded\";b:1;s:6:\"opened\";b:0;s:8:\"selected\";b:1;s:8:\"disabled\";b:0;}s:4:\"data\";O:8:\"stdClass\":0:{}s:6:\"parent\";s:1:\"3\";s:4:\"type\";s:4:\"acao\";}i:6;a:9:{s:2:\"id\";s:1:\"8\";s:4:\"text\";s:10:\"Visualizar\";s:4:\"icon\";s:20:\"fa fa-lock text-osun\";s:7:\"li_attr\";O:8:\"stdClass\":1:{s:2:\"id\";s:1:\"8\";}s:6:\"a_attr\";O:8:\"stdClass\":1:{s:4:\"href\";s:1:\"#\";}s:5:\"state\";O:8:\"stdClass\":4:{s:6:\"loaded\";b:1;s:6:\"opened\";b:0;s:8:\"selected\";b:1;s:8:\"disabled\";b:0;}s:4:\"data\";O:8:\"stdClass\":0:{}s:6:\"parent\";s:1:\"3\";s:4:\"type\";s:4:\"acao\";}}","grupo_id":1}

How can I unserializar only the column tree_funcao, keeping the other fields as they are ?

2 answers

1


To access the key tree_funcao, must first use a json_decode, (because, as far as I can tell, it’s a json) that will return a array associative, after that access key tree_funcao and use unserialize as follows:

<?php

$data = '{"id":3,"nome":"teste","tree_funcao":"a:7:{i:0;a:9:{s:2:\"id\";s:1:\"1\";s:4:\"text\";s:9:\"Dashboard\";s:4:\"icon\";s:23:\"fa  fa-folder text-osun\";s:7:\"li_attr\";O:8:\"stdClass\":1:{s:2:\"id\";s:1:\"1\";}s:6:\"a_attr\";O:8:\"stdClass\":2:{s:4:\"href\";s:1:\"#\";s:2:\"id\";s:8:\"1_anchor\";}s:5:\"state\";O:8:\"stdClass\":4:{s:6:\"loaded\";b:1;s:6:\"opened\";b:0;s:8:\"selected\";b:1;s:8:\"disabled\";b:0;}s:4:\"data\";O:8:\"stdClass\":0:{}s:6:\"parent\";s:1:\"#\";s:4:\"type\";s:5:\"grupo\";}i:1;a:9:{s:2:\"id\";s:1:\"2\";s:4:\"text\";s:14:\"Configura\u00e7\u00e3o\";s:4:\"icon\";s:23:\"fa  fa-folder text-osun\";s:7:\"li_attr\";O:8:\"stdClass\":1:{s:2:\"id\";s:1:\"2\";}s:6:\"a_attr\";O:8:\"stdClass\":2:{s:4:\"href\";s:1:\"#\";s:2:\"id\";s:8:\"2_anchor\";}s:5:\"state\";O:8:\"stdClass\":4:{s:6:\"loaded\";b:1;s:6:\"opened\";b:0;s:8:\"selected\";b:1;s:8:\"disabled\";b:0;}s:4:\"data\";O:8:\"stdClass\":0:{}s:6:\"parent\";s:1:\"#\";s:4:\"type\";s:5:\"grupo\";}i:2;a:9:{s:2:\"id\";s:1:\"3\";s:4:\"text\";s:8:\"Usu\u00e1rio\";s:4:\"icon\";s:34:\"fa fa-angle-double-right text-osun\";s:7:\"li_attr\";O:8:\"stdClass\":1:{s:2:\"id\";s:1:\"3\";}s:6:\"a_attr\";O:8:\"stdClass\":2:{s:4:\"href\";s:1:\"#\";s:2:\"id\";s:8:\"3_anchor\";}s:5:\"state\";O:8:\"stdClass\":4:{s:6:\"loaded\";b:1;s:6:\"opened\";b:0;s:8:\"selected\";b:1;s:8:\"disabled\";b:0;}s:4:\"data\";O:8:\"stdClass\":0:{}s:6:\"parent\";s:1:\"2\";s:4:\"type\";s:6:\"funcao\";}i:3;a:9:{s:2:\"id\";s:1:\"4\";s:4:\"text\";s:5:\"Criar\";s:4:\"icon\";s:20:\"fa fa-lock text-osun\";s:7:\"li_attr\";O:8:\"stdClass\":1:{s:2:\"id\";s:1:\"4\";}s:6:\"a_attr\";O:8:\"stdClass\":1:{s:4:\"href\";s:1:\"#\";}s:5:\"state\";O:8:\"stdClass\":4:{s:6:\"loaded\";b:1;s:6:\"opened\";b:0;s:8:\"selected\";b:1;s:8:\"disabled\";b:0;}s:4:\"data\";O:8:\"stdClass\":0:{}s:6:\"parent\";s:1:\"3\";s:4:\"type\";s:4:\"acao\";}i:4;a:9:{s:2:\"id\";s:1:\"5\";s:4:\"text\";s:6:\"Editar\";s:4:\"icon\";s:20:\"fa fa-lock text-osun\";s:7:\"li_attr\";O:8:\"stdClass\":1:{s:2:\"id\";s:1:\"5\";}s:6:\"a_attr\";O:8:\"stdClass\":1:{s:4:\"href\";s:1:\"#\";}s:5:\"state\";O:8:\"stdClass\":4:{s:6:\"loaded\";b:1;s:6:\"opened\";b:0;s:8:\"selected\";b:1;s:8:\"disabled\";b:0;}s:4:\"data\";O:8:\"stdClass\":0:{}s:6:\"parent\";s:1:\"3\";s:4:\"type\";s:4:\"acao\";}i:5;a:9:{s:2:\"id\";s:1:\"6\";s:4:\"text\";s:7:\"Excluir\";s:4:\"icon\";s:20:\"fa fa-lock text-osun\";s:7:\"li_attr\";O:8:\"stdClass\":1:{s:2:\"id\";s:1:\"6\";}s:6:\"a_attr\";O:8:\"stdClass\":1:{s:4:\"href\";s:1:\"#\";}s:5:\"state\";O:8:\"stdClass\":4:{s:6:\"loaded\";b:1;s:6:\"opened\";b:0;s:8:\"selected\";b:1;s:8:\"disabled\";b:0;}s:4:\"data\";O:8:\"stdClass\":0:{}s:6:\"parent\";s:1:\"3\";s:4:\"type\";s:4:\"acao\";}i:6;a:9:{s:2:\"id\";s:1:\"8\";s:4:\"text\";s:10:\"Visualizar\";s:4:\"icon\";s:20:\"fa fa-lock text-osun\";s:7:\"li_attr\";O:8:\"stdClass\":1:{s:2:\"id\";s:1:\"8\";}s:6:\"a_attr\";O:8:\"stdClass\":1:{s:4:\"href\";s:1:\"#\";}s:5:\"state\";O:8:\"stdClass\":4:{s:6:\"loaded\";b:1;s:6:\"opened\";b:0;s:8:\"selected\";b:1;s:8:\"disabled\";b:0;}s:4:\"data\";O:8:\"stdClass\":0:{}s:6:\"parent\";s:1:\"3\";s:4:\"type\";s:4:\"acao\";}}","grupo_id":1}';

$data_array = json_decode ($data, TRUE);
$tree_funcao = unserialize ($data_array['tree_funcao']);

print_r($tree_funcao);

all other fields are within the array shall be used only as follows:

$data_array['id']; // id
$data_array['nome']; // nome

and so on!

Ideone Online Example

References

  • so I’ve done and seen that it works. It turns out that this associative array is in a table field, but the other fields are not array. So how can I return the other fields too?

  • So just make $data_array['id'] so on @Wagnerfilho ... Got it.!?

  • @Wagnerfilho edited response!

  • I put an answer based on your answer, so I have given your answer. I have only one question, if you can help me. As you can see, inside the key tree_funcao in each index of the array, there is an item, called state. It is possible to go through all indexes and get the value selected of state of each travel index? [state] => stdClass Object&#xA; (&#xA; [loaded] => 1&#xA; [opened] => &#xA; [selected] => 1&#xA; [disabled] => &#xA; )

  • @Wagnerson has yes just do a for and then access state->selected ...

  • something like foreach ($item['tree_funcao'] as $tree_funcao => $item)&#xA; {&#xA; echo ($tree_funcao);&#xA; //echo ($tree_funcao['state']['selected']);&#xA; }

  • $tree_funcao['state']->selected @Wagnerfilho

  • Good morning, can you help me with this question ? https://answall.com/questions/291566

Show 3 more comments

0

public function get_data($permissao_id)
{
    $this->db->select('id, nome, permissoes tree_funcao, grupo_id');
    $this->db->from($this->tabela);
    $this->db->where('id',$permissao_id);
    $query = $this->db->get();
    $item = $query->row_array();

    $item['tree_funcao'] = unserialize($item['tree_funcao']);       
    return $item;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.