subikshan M
subikshan M

Reputation: 263

how to display array format in json string to array

Here i have one array object , in this array galleryImages is multiple images so i doing like this see below my code

My controller

public function getGalley()
    {
        date_default_timezone_set('Asia/Kolkata');
        //$loginType = $_POST['loginType'];
         $loginType = 1;
            if($loginType == 1)
            {
                $data = array(
                 "school_id" => $_POST['schoolId'],
                ); 

                $response= $this->Android_login_model->android_get_gallery($data);
                foreach ($response as $key => $value)
                    {
                        $response[$key]["galleryImages"]=  json_decode($value["galleryImages"],true);
                    }
                    print_r(json_encode($response));


            }
    }

My Model

public function android_get_gallery($params)
    { 
        $this->db->select('*');
        $this->db->where('status !=', '1');
        $this->db->where('school_id =',$params['school_id']);
        return $this->db->get('gallery')->result();
    }

In my controller print_r($response); means i getting answer like this

Array
(
    [0] => stdClass Object
        (
            [gallery_id] => 1
            [title] => Title 1
            [description] => gfhfg
            [galleryImages] => ["6be1954c4bec91fe26fb7447fc551782.jpeg","153651d989591e76444c92cf037d5ac4.jpg"]
            [reg_on] => 2017-04-03 12:21:59
            [created_by] => [email protected]
            [school_id] => 2
            [status] => 0
        )

    [1] => stdClass Object
        (
            [gallery_id] => 2
            [title] => Title 2
            [description] => sdfdsfsdfsdfsdf
            [galleryImages] => ["f356ceafa408e61cd1c62cfc39752b32.jpeg","a4fd29005696d92e0fc4cd3931454609.jpg","8eaaecdac1ff219192806acba7978a1b.jpg"]
            [reg_on] => 2017-04-03 12:23:37
            [created_by] => [email protected]
            [school_id] => 2
            [status] => 0
        )

    [2] => stdClass Object
        (
            [gallery_id] => 3
            [title] => Sports Day
            [description] => dsfsdfsdfs
            [galleryImages] => ["1036f71f4781a6f6f59a4e3aeef35624.jpg","c8c330f7dee14f58a35ecd671e9a220c.jpg","b17503ff2da34a749425ce6ecd253e45.jpg","4f89417eb0e7b987728243051000a301.jpg"]
            [reg_on] => 2017-04-07 07:09:24
            [created_by] => [email protected]
            [school_id] => 2
            [status] => 0
        )

)

My expected json output is

    [{"gallery_id":1,
"title":"Title 1",
"galleryImages":[{"6be1954c4bec91fe26fb7447fc551782.jpeg"},{"153651d989591e76444c92cf037d5ac4.jpg"}],
"reg_on":"2017-04-03 12:21:59",
"created_by":"[email protected]",
"school_id":2,"status":0
},

{
"gallery_id":2,
"title":"Title 2",
"galleryImages":[{
"f356ceafa408e61cd1c62cfc39752b32.jpeg"
},{"a4fd29005696d92e0fc4cd3931454609.jpg"},{"8eaaecdac1ff219192806acba7978a1b.jpg"}],
"reg_on":"2017-04-03 12:23:37",
"created_by":"[email protected]",
"school_id":2,"status":0
}
]

Upvotes: 0

Views: 71

Answers (1)

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26268

Here $response is an array of objects, so to convert it into your desired format you have to first convert it into array of array like:

$respArr = array();
foreach($response as $data)
{
    $respArr[] = $data;
}

after that use json_encode() on it like:

json_encode($respArr);

Upvotes: 2

Related Questions