Unserialize data in CodeIgniter?

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?

    
asked by anonymous 29.03.2018 / 22:03

2 answers

1

To access the tree_funcao key, you must first use a json_decode , ( because, as far as I can tell, it is a json ) that will return a array associative , after that access the tree_funcao key 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 array , just use it as follows:

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

and so on!

Example Online Ideone

29.03.2018 / 23:53
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;
}
    
02.04.2018 / 18:26