Reputation: 1596
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.
Upvotes: 0
Views: 145
Reputation: 13348
It is important to note here that json_decode
returns an instance of stdClass by default. Try using json_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:
revision
method, but you aren't using it when you need to. You should change $this->revision()
to $this->revision(true)
in your last_revision
method.revision()
method, there's not much of a point in json_encode
ing it, just to json_decode
the 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']);
Upvotes: 1
Reputation: 2733
You can change the edit_function() to:
public function edit_revision($return = false)
{
$galleryID = $this->input->post('galleryID');
$revisionID = $this->input->post('revisionID');
$data = array('revision_images' => $this->revision->get($galleryID, $revisionID) );
if($return)
return json_encode($data);
else
echo json_encode($data);
}
and then:
public function last_revision(true)
{
$allRevisions = json_decode($this->revision());
return end($allRevisions);
}
Upvotes: 1
Reputation: 147
Maybe you need convert that json output to an php array (json_decode() function), then you could get the last item with array_pop() function: https://php.net/array_pop
Upvotes: 0