Home > Software design >  Unserializing a serialized array from model class
Unserializing a serialized array from model class

Time:12-15

Currently I have a serialized array in my database table which looks something like this: a:35:s:5:"leads";s:4:"true";s:9:"all_leads";N;s:10:"unassigned";N;s:6:"active";s:4:"true";.... Now I'm trying to unserialize this in my array using the following code:

function unserialize_permissions($id)
{
    $this->db->where("roles_id",$id);
    $this->db->select("permissions");
    $this->db->from("crm_client_roles");
    $query = $this->db->get();
    return unserialize($query->result_array());
}

But this gives an error saying

unserialize(): Argument #1 ($data) must be of type string, array given

So to fix this I changed unserialize($query->result_array()) to unserialize("",$query->result_array()) which now gives bool(false) as the result. I'm not sure where I'm going wrong with this now.

CodePudding user response:

You need to pass in the specific field containing the serialized data, from the array returned by the query.

I'm not familiar with CodeIgniter or its ORM but I'd guess if it returns an associative array then

$result = $query->result_array(); 
unserialize($result["permissions"]);

would make sense.

  • Related