0

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
}
]
5
  • you have an array of objects, either convert all them to arrays and then encode or change your model where you have mentioned ->result() and change it to ->result_array() Commented Apr 10, 2017 at 5:44
  • try to form the result in you controller. You are just returning all model data to view. Commented Apr 10, 2017 at 5:47
  • can you update your answer i am getting @ Nishant Nair Commented Apr 10, 2017 at 5:49
  • @subikshanM Hope my post will help you out. Commented Apr 10, 2017 at 5:58
  • @subikshanM In your comments you said you need [ { "1.jpg" }, { "2.jpg" } ] and this is not valid json { "1.jpg" } is object there should be some key for a value like this {"someKey": "1.jpg" } check you expected json here json.parser.online.fr Commented Apr 10, 2017 at 9:06

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

i tried your code it is not working check my updated code like that answer i am getting , slashes is adding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.