【问题标题】:Not getting the array on JSON没有在 JSON 上获取数组
【发布时间】:2020-07-04 15:50:49
【问题描述】:

您好,我正在尝试分解 JSON 数据

 ** {
    "message": "gradeExam.php",
    "id": "171",
    "student_id": "dfd",
    "questions": [{
          "question_id": "0",
          "student_input": "def doubly_"
        },
        {
          "question_id": "1",
          "student_input": "asd"
        }
    ]
}**

使用 PHP

$json = file_get_contents('php://input');
$_POST = json_decode($json, true);

$input = $_POST["questions"];
foreach($input as $questions)
{
$quest_id[] = $questions["question_id"];
$student_input[] = $questions["student_input"];
}
echo $quest_id;
echo $student_input;

但不断收到...的响应。 ArrayArrayArray[]

我正在尝试循环并获取question_id:0,student_input:def double_,question_id:1,student_input:asd的数据。

我做错了什么?

【问题讨论】:

  • 不能回显数组,使用var_dump($quest_id, $student_input);
  • array(2) { [0]=> string(1) "0" [1]=> string(1) "1" } array(2) { [0]=> string( 11) "def doubly_" [1]=> string(3) "asd" } ---这是我得到的回报
  • 这不是你想要的吗?第一个是问题 ID 数组,第二个是输入数组。

标签: php sql arrays json


【解决方案1】:

除了这两行之外,你所做的一切都是正确的。

echo $quest_id;
echo $student_input;

由于 $quest_id 和 $student_input 是数组,我们不能使用 echo 函数。相反,您可以使用下面的 print_r 函数

print_r($quest_id);
print_r($student_input);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    • 2015-08-22
    • 1970-01-01
    • 2013-05-15
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    相关资源
    最近更新 更多