Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 访问数组值为空_Php - Fatal编程技术网

Php 访问数组值为空

Php 访问数组值为空,php,Php,您好,我已经解码了一个发送到服务器的json字符串,我正在尝试从他那里获取值 我的问题是无法从内部数组中获取值 这是我的代码: <?php $post = file_get_contents('php://input'); $arrayBig = json_decode($post, true); foreach ($arrayBig as $array) { $exercise = $array['exercise']; $re

您好,我已经解码了一个发送到服务器的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);


    }

?>
如果我能看到数组中不是空的,为什么
$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']