How to generate json response using php

前端 未结 2 1467
余生分开走
余生分开走 2021-01-16 12:23

How to generate json response using php

In the model:

public function groups($getGroupId) {
        $cols = array(\'group_id\',\'name\');
        $sq         


        
2条回答
  •  死守一世寂寞
    2021-01-16 13:19

    Since you are using Zend Framework, I recommend you to use Zend_Json. Zend_Json is a pretty useful component to use in order to format Json from any supported format (object, array, xml...).

    Zend_Json::decode() and Zend_Json::encode() will allow you to encode and decode Json and prettyPrint() is used to make your output prettier.


    Edit: As Svish said, your two examples doesn't look alike, so it's kind of hard to guess what you want to put inside your tree.

    What you need is to create your own array, so you can make it look like the way you want.

    For example, let's say you only want one row from your database in your tree, then your array would be something like this:

    $v = array(
           array(
             "data" => array("icon" => "ICON",
                             "title" => $row->name),
             "attr" => array("rel" => "REL",
                             "title" => "TITLE", 
                             "id" => $row->group_id),
              "state" =>     "closed"));
    echo Zend_Json::encode($v);
    

    These lines should echo something like in your examples.

    To make it works with your fetchAll(), a simple foreach will do it.

提交回复
热议问题