Jump to content

is there any built-in function or api to convert a result array into a json


adrianmak
 Share

Recommended Posts

For example, I find all pages with template name ABCDE

$items = $this->pages->find("template=ABCDE");
 foreach ($items as $item) {
                echo "<pre>" . var_dump($item) . "</pre>";
             }

}

I var dump for each item, it has many other irrelevant data.

Link to comment
Share on other sites

'those many other irrelevant' data are actually very relevant :-)..Your find returns several Page objects (OK, could be one if only one hit found) each with lots of properties about the object. Because of this, var_dump() is not very helpful. What data exactly do you need from your find? Is that what you want to encode to json? Then you could do something like this...

$myItems = array();
$items = $this->pages->find("template=ABCDE");
foreach ($items as $item) $myItems[] = array('id'=> $item->id, 'title' => $item->title);
$myItemsJSON = json_encode($myItems);//
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...