0

我在 Yii 框架中工作。我有以下功能-

public function actionGetPublicPoll()
{
    $model=new poll();
    $polltype=Poll::model()->findAllByAttributes(array("pollTypeId"=>1));
    foreach ($polltype as $poll)
    {   
        if($poll->isActive==1 && $poll->isPublished==0)
        {
            echo "pollId is=".$poll->pollId;
            $Id=$poll->pollId;
            $option=Polloption::model()->findAllByAttributes(array("pollId"=>$poll->pollId));

            foreach ($option as $option1)
            {
                echo $option1->optionId."</br>";
                echo $option1->option;
                $optionList[]=$option1->option;
            }
        } 
    }
    echo CJSON::encode(array("PollId"=>$Id,"Question"=>$poll->pollQuestion,"options"=>$optionList));
}

所以我正在发送pollQuestion及其json格式选项。从上面,我得到的输出为-

{"PollId":"3","Question":"Which is the biggest district in india ","options":["sachin tendulakar","Yuwraj singh","Rohit sharma","Mahendrasing dhoni"]}

但我想要 json 格式:

{"PollId":"3","Question":"Which is the biggest district in india ","options":["option":"sachin tendulakar","option":"Yuwraj singh","option":"Rohit sharma","option":"Mahendrasing dhoni"]}

那么我需要修改什么?请帮我....

4

1 回答 1

0

改变

$optionList[]=$option1->option;

$optionList[]=array('option'=>$option1->option);

...................................

foreach ($option as $option1)
{
                echo $option1->optionId."</br>";
                echo $option1->option;
                $optionList[]=array('option'=>$option1->option);
}

……………………………………………………………………………………

你会JSON得到

{"PollId":"3","Question":"Which is the biggest district in india ",
"options":[{"option":"sachin tendulakar"},{"option":"Yuwraj singh"},{"option":"Rohit sharma"},{"option":"Mahendrasing dhoni"}]}
于 2012-12-20T12:43:12.150 回答