2

Am trying to build a JSON array from a php array This is my function in my controller

function latest_pheeds() {
            if($this->isLogged() == true) {
            $this->load->model('pheed_model');
            $data = $this->pheed_model->get_latest_pheeds();
            $last = end($data);
            echo "[";
                for($i = 0; $i < count($data); $i++) {
                    echo '{"user_id":"'.$data[$i][0]['user_id'].'",';
                    echo '"pheed_id":"'.$data[$i][0]['pheed_id'].'",';
                    echo '"pheed":"'.$data[$i][0]['pheed'].'",';
                    echo '"datetime":"'.$data[$i][0]['datetime'].'",';
                        if($i == count($data)) {
                        echo '"comments":"'.$data[$i][0]['comments'].'"}';
                        }else {
                            echo '"comments":"'.$data[$i][0]['comments'].'"},';
                        }
                    }

                echo "]";
            }
            return false;
    }

It returns a json array like this

[{"user_id":"9","pheed_id":"2","pheed":"This is my first real pheed, its got potential ","datetime":"1313188898","comments":"0"},{"user_id":"9","pheed_id":"11","pheed":"My stomach being hurting all day","datetime":"1313422390","comments":"0"},{"user_id":"9","pheed_id":"11","pheed":"My stomach being hurting all day","datetime":"1313422390","comments":"0"},{"user_id":"9","pheed_id":"10","pheed":"Thank God for stackoverflow.com ","datetime":"1313358605","comments":"0"},]

But i cant seem to access it with jquery

7
  • 1
    do you need that comma at the end Commented Aug 17, 2011 at 20:11
  • How are you trying to access it with jQuery? Commented Aug 17, 2011 at 20:13
  • As suggested, the comma at the end is breaking the json decoding. Also, as suggested in the asker by @derekerdmann, use json_encode for this, it is what it is meant for. Commented Aug 17, 2011 at 20:13
  • Yes, just give your PHP array to json_encode() function. Commented Aug 17, 2011 at 20:13
  • I use json_encode all the time between PHP and javascript. You should revisit your code and do the same. No need to wrap your own. Commented Aug 17, 2011 at 20:37

2 Answers 2

8

I believe the problem rests with the trailing comma at the end of your array.

Rather than try to encode it yourself, use PHP's json_encode function. It's been tested and verified many, many times, so you don't have to reinvent the wheel.

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

1 Comment

@MrFoh: that is a different issue. I would suggest setting your headers to indicate that you are returning JSON.
1

Already voted up @derekerdmann, but thought I would add...

Your code will work, if you change:

if($i == count($data)) {

to

if($i == count($data) - 1) {

But, don't do that. If you are just putting everything from each member of the $data array into the json, then you should be able to just json_encode($data). If you are only pulling out certain parts, then build up a secondary array of your filtered data and json_encode that instead.

function latest_pheeds() {
    if($this->isLogged() == true) {
        $this->load->model('pheed_model');
        $data = $this->pheed_model->get_latest_pheeds();
        $filtered_items = array();
        foreach ($data as $member) {
            $filtered_item = array();
            $filtered_item['user_id'] = $member['user_id'];
            $filtered_item['pheed_id'] = $member['pheed_id'];
            ...
            ...
            $filtered_items[] = $filtered_item;
        }
        echo json_encode($filtered_items);
    }
    return false;
}

Comments

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.