Edit
Thanks for all the input on this, I did find error in my question so modifying now. Sorry for that.
I am trying to figure out how to return the last object in the JSON string I have rendered. The two functions I am working with:
public function revision($return = false)
{
$id = $this->input->post('galleryID');
$data = array('revision_count' => $this->revision->count_revision($id) );
if($return){
return json_encode($data);
}
else {
echo json_encode($data);
}
}
public function last_revision()
{
$allRevisions = json_decode($this->revision(),true);
return end($allRevisions);
}
The issue is that end() returns error stating that 1st parameter should be array.
Thanks for any help on this.
It is important to note here that
json_decodereturns an instance of stdClass by default. Try usingjson_decode($jsonstring, true)to return the JSON as a PHP associative array.However, You haven’t included what the
$this->revision()method does. Could you possibly show that portion of the code, since that is the function you are getting a return value from?Edit:
Alright, after we saw the right function in your code, here are a couple of things I would like to say:
revisionmethod, but you aren’t using it when you need to. You should change$this->revision()to$this->revision(true)in yourlast_revisionmethod.revision()method, there’s not much of a point injson_encodeing it, just tojson_decodethe result. Just pass back the raw data array.Once you have changed both of these things, this should work:
$allRevisions = $this->revision(true); return end($allRevisions['revision_count']);