Php 访问数组值为空
您好,我已经解码了一个发送到服务器的json字符串,我正在尝试从他那里获取值 我的问题是无法从内部数组中获取值 这是我的代码:Php 访问数组值为空,php,Php,您好,我已经解码了一个发送到服务器的json字符串,我正在尝试从他那里获取值 我的问题是无法从内部数组中获取值 这是我的代码: <?php $post = file_get_contents('php://input'); $arrayBig = json_decode($post, true); foreach ($arrayBig as $array) { $exercise = $array['exercise']; $re
<?php
$post = file_get_contents('php://input');
$arrayBig = json_decode($post, true);
foreach ($arrayBig as $array)
{
$exercise = $array['exercise'];
$response["exercise"] = $exercise;
$response["array"] = $array;
echo json_encode($response);
}
?>
如果我能看到数组中不是空的,为什么$array['exercise']
为空
谢谢。因为
[{…}]
当您解码数组
键时,您将得到一个数组中的数组
因此:
应该是:
$exercise = $array[0]['exercise'];
请参阅。,因为
[{…}]
在解码数组
键时,您将在数组中获得一个数组
因此:
应该是:
$exercise = $array[0]['exercise'];
查看。查看
$response['array']
的结果,看起来$array
实际上是这样的
[['exercise' => 'foo', 'reps' => 'foo']]
也就是说,嵌套在数字数组中的关联数组。在盲目赋值之前,您可能应该进行一些值检查,但为了简洁起见
$exercise = $array[0]['exercise'];
从查看
$response['array']
的结果来看,$array
实际上是这样的
[['exercise' => 'foo', 'reps' => 'foo']]
也就是说,嵌套在数字数组中的关联数组。在盲目赋值之前,您可能应该进行一些值检查,但为了简洁起见
$exercise = $array[0]['exercise'];
您应该执行
var\u转储($arrayBig)
。您可能会看到阵列中有另一个阵列。我建议您启用显示错误
,并将错误报告
设置为E\u ALL
。对于$array['exercise']
您有一个未定义的索引错误,您应该执行var\u转储($arrayBig)
。您可能会看到阵列中有另一个阵列。我建议您启用显示错误
,并将错误报告
设置为E\u ALL
。$array['exercise']