Json Encode Decode in Codeigniter


Create function in library, for once write and multiple use so create function and call at any function all over in project. 


Below is the Library controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Authenticator
{
        public function url_encode_arr($input)
{
return urlencode(base64_encode(serialize($input)));
}
public function url_decode_arr($input)
{
return unserialize(base64_decode(urldecode($input)));
}
}
?>

Save above file  in Project >> Application >> libraries as  Authenticator.php
 
And Call in controller file where you need as  $this->load->library('authenticator');


To Encode Array into Json Below is the example

<?php
     $arr =array("user_id"=>"101", "name"=>"Guidephp");
     $encoded_json_arr = $this->authenticator->url_encode_arr($arr); 
?>


To Decode Json to Array, Below is the example

<?php 
    $decoded_json_arr = $this->authenticator->url_decode_arr($encoded_json_arr);
    $user_id= $decoded_json_arr["user_id"];
    $name= $decoded_json_arr["name"];
?>





Json Encode Decode in Codeigniter Json Encode Decode in Codeigniter Reviewed by Admin on July 12, 2020 Rating: 5

No comments:

Powered by Blogger.