首页 > 解决方案 > PHP代码,试图回显设置为解码的json数组的变量

问题描述

我正在尝试将变量设置为下面的“question_id”和“weight”,但是当我尝试回显我设置为“question_id”和“weight”的变量时,没有任何回声。

var_dump($data);

array(3) {
  [
    "examQuestions"
  ]=>
  array(2) {
    [
      0
    ]=>
    array(2) {
      [
        "question_id"
      ]=>
      string(2) "88"
      [
        "weight"
      ]=>
      string(1) "5"
    }
    [
      1
    ]=>
    array(2) {
      [
        "question_id"
      ]=>
      string(2) "89"
      [
        "weight"
      ]=>
      string(1) "5"
    }
  }

回显 $quest_id 和 $quest_points 的代码

 <? php
 $json = file_get_contents('php://input');
$data = json_decode($json, true);
var_dump($data);
$quest_id = $data["question_id"];
$quest_points = $data["weight"];
echo $quest_id;
echo $quest_points;
?>

标签: phpmysqlpost

解决方案


尝试:

<?php
$json = file_get_contents('php://input');
$data = json_decode($json, true);
var_dump($data);
$questions = $data["examQuestions"];
foreach($questions as $question) {
    $quest_id = $question["question_id"];
    $quest_points = $question["weight"];
    echo $quest_id;
    echo $quest_points;
}

推荐阅读