【问题标题】:Convert a questionnaire to an object将问卷转换为对象
【发布时间】:2020-11-11 05:30:09
【问题描述】:

希望你能帮助我,我之前确实想出了一些递归,但现在不是其中之一。作为问卷调查的结果,我有这样的数组:

Array
(
    [Q1] => Array
        (
            [A3] => Array
                (
                    [Q11] => Array
                        (
                            [A11] => Array
                                (
                                    [Q111] => A112
                                )
                        )
                    [Q12] => Array
                        (
                            [A23] => Stackoverflow
                        )
                )
        )
    [Q2] => A5
)

这些是问题和答案(我不能依赖它们的索引或前缀,但它们总是成对出现),如果答案是数组,它有子问题。 Q1 有 A3 作为答案,它有 A11 作为 Q11 的嵌套答案,最后是 A112 作为该分支中 Q111 的最终答案。但是 Q1 也有一个并行子问题,选择了 A23,因为它是“其他:”选项,所以用户输入了文本。最后,A5答案被选为第二题。

为了更快的输入,这是 JSON 中的数组:

    {"Q1":{"A3":{"Q11":{"A11":{"Q111":"A112"}},"Q12":{"A23":"Stackoverflow"}}},"Q2":"A5"}

所以现在,我需要在这样的对象中得到结果:

{
    "questions": [{
            "questionId": "Q1",
            "answers": [{
                "answerId": "A3",
                "questions": [{
                        "questionId": "Q11",
                        "answers": [{
                            "answerId": "A11",
                            "questions": [{
                                "questionId": "Q111",
                                "answers": [{
                                    "answerId": "A112"
                                }]
                            }]
                        }]
                    },
                    {
                        "questionId": "Q12",
                        "answers": [{
                            "answerId": "A23",
                            "answerPhrase": "Stackoverflow"
                        }]
                    }
                ]
            }]
        },
        {
            "questionId": "Q2",
            "answers": [{
                "answerId": "A5"
            }]
        }
    ]
}

我确信递归相对简单,但在尝试迷路之后,我尝试了 JSON,结果更糟。我提醒你 Q 和 A 是成对出现的,所以我相信递归应该有两个嵌套循环,但你可能知道得更好——我不能依赖索引值。在此先感谢 PHP 向导!

【问题讨论】:

    标签: php arrays json object recursion


    【解决方案1】:

    请检查这是否能满足您的需求。它是使用递归函数完成的。与我的结果和您正在寻找的结果唯一不同的是 Q1 和 Q2 处于同一水平,而不是作为答案下的另一个对象。根据您的其余问题,我认为这是一个错字,我所做的将是正确的。

    $response = json_decode('{"Q1":{"A3":{"Q11":{"A11":{"Q111":"A112"}},"Q12":{"A23":"Stackoverflow"}}},"Q2":"A5"}', true);
    
    function processQuestions($questions)
    {
      $ret = array();
      if(!empty($questions))
      {
        foreach($questions as $question => $answer)
        {
          $ret[] = array(
              'questionId' => $question,
              'answers' => processAnswers($answer),
          );
        }
      }
      return $ret;
    }
    
    function processAnswers($answers)
    {
      if(!is_array($answers))
      {
        return array(array('answerId'=>$answers));
      }
    
      $ret = array();
      if(!empty($answers))
      {
        foreach($answers as $answerId=>$answer)
        {
          $ans = array('answerId'=>$answerId);
          if(is_array($answer))
          {
            $ans['questions'] = processQuestions($answer);
          }
          else
          {
            $ans['answerPhrase'] = $answer;
          }
          $ret[] = $ans;
        }
      }
      return $ret;
    }
    
    $result = array('questions'=>processQuestions($response));
    

    【讨论】:

    • 嗨@Mark,这几乎是完美的!非常优雅的方法,两个函数相互调用。只是现在答案是对象数组——即使那里只有 answerId。
    • 我已经编辑了代码,是现在吗?在 processAnswers 中更改了一行,第 24 行。
    • 完美。谢谢!是的,生成的 JSON 是错误的,现在已修复。
    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2017-06-16
    • 2014-04-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 2021-05-15
    相关资源
    最近更新 更多