如何从Mysql中获取json数据并将其转换为CodeIgniter中的数组

时间:2022-02-28 12:51:34

I have get the MySQL result as below format. I want to send this as JSON array format in an app.The keyword data are stored JSON array format in db. How to send this as JSON array ?

我得到了以下格式的MySQL结果。我想在app中将其作为JSON数组格式发送。关键字数据在db中以JSON数组格式存储。如何将此作为JSON数组发送?

MySQL RESULT

Array 
(
    [groups] => Array
        (
            [0] => stdClass Object
                (
                    [id] => 7
                    [category_name] => Serial
                    [group_name] => test group
                    [description] => efrt
                    [image] => 9955_1475731912.png
                    [time] => 07:30 AM
                    [member_count] => 0
                    [keyword] => [{"key_id":"1","key_name":"HBO"},
                                 {"key_id":"2","key_name":"BOLLYWOOD HUNGAMA"},
                                 {"key_id":"3","key_name":"SERIALS"}]
                )

        )

        "member_count": "0"
    }]
}

JSON ENCODE FORMAT

JSON编码格式

{
  "groups": [
    {
      "id": "7",
      "category_name": "Serial",
      "group_name": "test group",
      "description": "efrt",
      "image": "9955_1475731912.png",
      "time": "07:30 AM",
      "member_count": "0",
      "keyword": "[{\"key_id\":\"1\",\"key_name\":\"HBO\"},
                  {\"key_id\":\"2\",\"key_name\":\"BOLLYWOOD HUNGAMA\"},
                  {\"key_id\":\"3\",\"key_name\":\"SERIALS\"}]"
    }
  ]
}

3 个解决方案

#1


1  

First create a php array in the way you want to encode in-side the function or use the below sample in model (which extends CI_Model)

首先以您希望在函数内部编码的方式创建一个php数组,或者在模型中使用以下示例(扩展CI_Model)

$query_result = $result->result(); //for get result array 
return json_encode($query_result);

sample

    $array = array("test"=>"hhh",array("us"=>"letter","test"=>"ttt"));
    //print_r(json_encode($array));
    return json_encode($array);

#2


0  

I use this method for my Question

我将此方法用于我的问题

    foreach ($result['groups'] as $key ) {
                $json = $key->keyword;
                $keys = json_decode($json);
                $arrayName = array();
                foreach ($keys as $value) {
                     $key_id = $value->key_id;
                     $key_name = $value->key_name;
                     $arrayName[] = array('key_id' =>$key_id ,'key_name'              =>$key_name);
                }
    $key->keyword =  $arrayName;
 }
 $result['success']= 1; 
 echo json_encode($result);
 exit();

#3


0  

$this->output
    ->set_content_type('application/json')
    ->set_output(json_encode($result->result()->groups));

I think this will solve it

我想这会解决它

#1


1  

First create a php array in the way you want to encode in-side the function or use the below sample in model (which extends CI_Model)

首先以您希望在函数内部编码的方式创建一个php数组,或者在模型中使用以下示例(扩展CI_Model)

$query_result = $result->result(); //for get result array 
return json_encode($query_result);

sample

    $array = array("test"=>"hhh",array("us"=>"letter","test"=>"ttt"));
    //print_r(json_encode($array));
    return json_encode($array);

#2


0  

I use this method for my Question

我将此方法用于我的问题

    foreach ($result['groups'] as $key ) {
                $json = $key->keyword;
                $keys = json_decode($json);
                $arrayName = array();
                foreach ($keys as $value) {
                     $key_id = $value->key_id;
                     $key_name = $value->key_name;
                     $arrayName[] = array('key_id' =>$key_id ,'key_name'              =>$key_name);
                }
    $key->keyword =  $arrayName;
 }
 $result['success']= 1; 
 echo json_encode($result);
 exit();

#3


0  

$this->output
    ->set_content_type('application/json')
    ->set_output(json_encode($result->result()->groups));

I think this will solve it

我想这会解决它