php - 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;
?>
解决方案
尝试:
<?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;
}
推荐阅读
- html - 是否可以将变量从 HTML (index.html) 发送到 Reactjs?
- flutter - 为什么我的 Flutter 项目中没有构建文件
- sql - 执行包含日期变量的 openquery
- scala - 单元测试在 maven 中运行良好,但在与 IntelliJ 一起使用时无法运行
- python - 使用 Django 正确组装饮料数据库(多对多)
- arrays - Ruby - Enumerable::group_by 和空字符串
- c# - 使用泛型进行反射
- flutter - RenderFlex 在底部溢出了 388 个像素,溢出的 RenderFlex 的方向为 Axis.vertical
- bash - 如何使用 zenity、pv 和 dd 将密码传送到 sudo
- solrj - 如何在 HttpSolrClient 中实现连接池?