0

I have a model that sends an error response to the controller in CodeIgniter that then is passed to the view which is just a JSON encoder. Here is the array from the model.

return $posts[] = array('complete'=>0,'error'=>1003, 'message'=>'Username already exists');

The issue I am having is that I need those square brackets after the $posts variable because sometimes I need an array of errors. However when I pass the single array to the view it encodes the JSON without the square brackets but when I have multiple arrays it includes the square brackets, I need the square brackets in the JSON every time. Here is the Controller...

$data['data'] = $this->logins_model->signup($post_data);  
$this->load->view('json', $data);  

Here is the view...

header('Content-type: application/json');  
$response['response'] = $data;  
echo json_encode($response); 

I need the JSON response to look like this

{
    "response": [
        {
            "complete": 0, 
            "error": 1003, 
            "message": "Username already exists"
        }
    ]
}  

NOT THIS!

{
    "response": {
        "complete": 0, 
        "error": 1003, 
        "message": "Username already exists"
    }
}
1
  • 2
    json formatted string does not match the array that is returned by your model (and json_encoded later). Post relevant code, please. Commented May 24, 2013 at 14:19

2 Answers 2

1

Since you want to get array in json you should be having it in php array as well (i.e. data-structures should meet). So $response['response'] = $data; should be $response['response'] = array($data);

In your example var_dump($response); gives:

array(1) {
  ["response"]=>
  array(3) {
    ["complete"]=>
    int(0)
    ["error"]=>
    int(1003)
    ["message"]=>
    string(23) "Username already exists"
  }
}

As you see $response['response'] is an object for json.

When you replace $response['response'] = $data; with $response['response'] = array($data); your data-structure, which you want to convert in json will become:

array(1) {
  ["response"]=>
  array(1) {
    [0]=>
    array(3) {
      ["complete"]=>
      int(0)
      ["error"]=>
      int(1003)
      ["message"]=>
      string(23) "Username already exists"
    }
  }
}

That will give you desired output because json_encode will expect that there might be another items in $response['response'].

Demo

Edit Your model should be returning one dimensional array. For example:

return array('complete'=>0,'error'=>1003, 'message'=>'Username already exists');

And you should assign it to another array that is holding all error messages:

$data['data'][] = $this->logins_model->signup($post_data);
$this->load->view('json', $data);  

Demo 2

Sign up to request clarification or add additional context in comments.

7 Comments

That works for if there is one error message but if there is an multiple error messages then there are double square brackets.
the issue is the array of error arrays is built in the logins_model and returned there, it can't be combined with other error arrays with the $data variable. For example for($i = 0; $i<10; $i++) { $posts[] = array('complete'=>0,'error'=>1003, 'message'=>'Username already exists'); } return $posts;
@StuartTheY If error array is constructed like this, you should get correct output. Let me post another demo. codepad.viper-7.com/qzK2GM
yes but now if you put array() around the $data variable on line 14 of your last demo there will be an extra set of brackets, see what I mean? It needs to be able to support a single array and arrays of arrays with just a single set of square brackets around it.
Oh well I guess it works for a single array if I break it up into two lines for some reason like this. ` $posts[] = array('complete'=>0, 'error'=>1003, 'message'=>'Username already exists'); return $posts;`
|
0

In your view define $post as an array and remove tha square brackets from there. To check your results in view use print_r instead of echo. Which will show exactly how many data is retrieved.

1 Comment

I need the square brackets in the model because sometimes I will build an array of arrays adding them on to the posts array and I leave out the [] then the array with only have the last item added to it.

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.