您好我已经解码了我发送到服务器的json字符串,我试图从他那里获取值。
我的问题是我无法从内部数组中获取值。
这是我的代码:
<?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);
}
?>
当我从$response
得到答案时,我得到了这个值:
{"exercise":null,"array":[{"exercise":"foo","reps":"foo"}]}
如果我在数组
中看到不为null,为什么$array['exercise']
为null
感谢。
答案 0 :(得分:2)
由于[{...}]
,您在解码array
密钥时会在数组中获取数组。
所以:
$exercise = $array['exercise'];
应该是:
$exercise = $array[0]['exercise'];
请参阅example here。
答案 1 :(得分:2)
通过查看$response['array']
的结果,看起来$array
实际上就是这个
[['exercise' => 'foo', 'reps' => 'foo']]
,即嵌套在数字数组中的关联数组。您应该在盲目分配值之前进行一些值检查,但为了简洁起见......
$exercise = $array[0]['exercise'];