首页 > 解决方案 > 将问卷转换为对象

问题描述

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

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 向导!

表格截图

标签: phparraysjsonobjectrecursion

解决方案


请检查这是否能满足您的需求。它是使用递归函数完成的。与我的结果和您正在寻找的结果唯一不同的是 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));

推荐阅读