亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

擺脫 ?: PHP 中的運算符

擺脫 ?: PHP 中的運算符

PHP
一只斗牛犬 2022-05-27 10:19:02
我對用于調查的 PHP 代碼有疑問。該代碼僅采用一個值“answer”,如果沒有答案,則采用“otherAnswer”。我需要它同時接受“答案”和“其他答案”這是代碼。感謝幫助。protected function convertRequestToUserAnswersArray(){    $answers = [];    if ($this->request->hasArgument('answers')) {        /** @noinspection PhpUnhandledExceptionInspection */        $requestAnswers = $this->request->getArgument('answers');        /** @noinspection PhpWrongForeachArgumentTypeInspection */        foreach ($requestAnswers as $questionUid => $requestAnswer) {            $answers[$questionUid] = $requestAnswer['answer'] ?: $requestAnswer['otherAnswer'];        }    }    return $answers;}
查看完整描述

3 回答

?
慕碼人2483693

TA貢獻1860條經驗 獲得超9個贊

假設這$answers[$questionUid]是一個數組,您可以嘗試將值推送到它:


/** @noinspection PhpWrongForeachArgumentTypeInspection */

foreach ($requestAnswers as $questionUid => $requestAnswer) {

    $answers[$questionUid] = $requestAnswer['answer'] . '----' . $requestAnswer['otherAnswer'];

}

希望這可以幫助。


查看完整回答
反對 回復 2022-05-27
?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

正如你所說,你想要兩個值然后你可以存儲到數組中,如下所示


$answers[$questionUid][] = $requestAnswer['answer'];

$answers[$questionUid][] = $requestAnswer['otherAnswer'];

通過使用上面的腳本$answers將有兩個值反對$questionUid


查看完整回答
反對 回復 2022-05-27
?
largeQ

TA貢獻2039條經驗 獲得超8個贊

我們不知道$answers將如何使用。因為返回 answer 和 otherAnswer 都需要$answers是數組。這意味著$answers必須重構任何用戶。

像這樣的東西可能會起作用:

$answers[$questionId] = ["answer" => $requestAnswer["answer"], "otherAnswers" => requestAnswer["otherAnswer"] ]

只需為密鑰存在添加一些檢查機制。

或者您可以按照另一個 SO 答案中的建議將其連接起來。


查看完整回答
反對 回復 2022-05-27
  • 3 回答
  • 0 關注
  • 173 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號