0

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.

1
  • 1
    edit_revision doesn't return anything, it outputs the json encoded array $data. Commented Dec 7, 2012 at 19:53

3 Answers 3

1

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:

  • You have added a $return parameter to your 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.
  • If you're going to return data from the revision() method, there's not much of a point in json_encodeing 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']);

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

2 Comments

Thanks, I have modified my question your comment helped discover a fault in my original question.
Thanks Colin - well explained and again sorry for the confusion in my orginal question.
1

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);
}

4 Comments

thanks for the help on this. $allRevisions is not being returned as array, thus end() is throwing error.
See my last edit, just add the paramater true to the last_revision call.
thanks, that didn't do the trick, even if I set $return = true, I am still getting end() parameter 1 not an array.
this is the answer if you could modify your code to my updated question and pass true to revision() -- Sorry for the mistake in code in original question $this->revision(true)
0

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

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.