0

我正在使用 extjs+yii 框架。我的客户端在 extjs 中,服务器端设计在 yii 框架中。所以 extjs 的表单输入通过 json 传入 yii framewok。这个 json 在 yii 控制器动作中被接受为-

 public function actionSetUserAnswer()
              {
     $postData = json_decode(file_get_contents("php://input"), true);
                 $clientData = $postData['data'];
                  }

extjs 发送的实际 json 数据是-

'{"data":[{"optionId":"","questionId":"1","isAnswer":"","option":"Aus","media":"","keyword":"","mediaTypeId":"","id":null},{"optionId":"","questionId":"2","isAnswer":"","option":"india","media":"","keyword":"","mediaTypeId":"","id":null},{"optionId":"","questionId":"3","isAnswer":"","option":"England","media":"","keyword":"","mediaTypeId":"","id":null},{"optionId":"","questionId":"4","isAnswer":"","option":"Srilanka","media":"","keyword":"","mediaTypeId":"","id":null}]}';

因此,在 yii 中,如何使用 $clientdata 变量访问 json 的这些单独字段,例如 option、questionId 等。

4

1 回答 1

0

你可以只使用数组下标

$option = $clientData[0]['option']

或者

$questionId = $clientData[0]['questionId']

如果要检索用于 in 的多个值findByAttributes,则需要一个值数组。您首先questionId从 JSON 中检索 s

function get_question_id($elem) {
    return $elem['questionId'];
}

$questionIds = array_map('get_question_id', $clientData);

然后在findByAttributes

$record=Question::model()->findByAttributes(array("questionId"=>$questionIds));

更新合并您的最后评论:

static public function get_question_id($elem)
{
    return $elem['questionId'];
}

public function actionSetUserAnswer() {
    $clientData = '{"data":[{"optionId":"","questionId":"1","isAnswer":"","option":"Au‌​s","media":"","keyword":"","mediaTypeId":"","id":null},...]}';
    $obj = json_decode($clientData['data']);
    $questionIds = array_map('get_question_id', $obj);
    var_dump($questionIds);
    $records = Question::model()->findByAttributes(array("questionId"=>$questionIds));
}
于 2013-02-06T08:10:43.747 回答