Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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中的数组解码json_Php_Json - Fatal编程技术网

用php中的数组解码json

用php中的数组解码json,php,json,Php,Json,我在从json获取数据时遇到问题。我无法更改它在数据库中的保存方式,因为它是由框架生成的,之后读取字段生成。我的json如下所示: {"102":{"textinput":{"comment":"2"}},"104":"34"} OR {"78":{"textinput":{"comment":"1"}},"82":"34"} Comment value是我从这个json获取的序列号。我试过: $json_sn = json_decode($customer_json_sn, true)

我在从json获取数据时遇到问题。我无法更改它在数据库中的保存方式,因为它是由框架生成的,之后读取字段生成。我的json如下所示:

{"102":{"textinput":{"comment":"2"}},"104":"34"}

 OR

{"78":{"textinput":{"comment":"1"}},"82":"34"}
Comment value是我从这个json获取的序列号。我试过:

$json_sn = json_decode($customer_json_sn, true);
$snr = $json_sn['78']['textinput']['comment'];
但这不是我需要的解决方案,因为我永远不知道第一个数字键的值,我不能依赖它。如有任何帮助,我们将不胜感激。

如何:

$snr_array = array()
foreach ($json_sn as $key)
    snr_array[] = $key['textinput']['comment'];
编辑:我刚刚意识到你可能只需要/得到一条评论:

$key = key($json_sn);
$snr = $json_sn[$key]['textinput']['comment'];
那么:

$snr_array = array()
foreach ($json_sn as $key)
    snr_array[] = $key['textinput']['comment'];
编辑:我刚刚意识到你可能只需要/得到一条评论:

$key = key($json_sn);
$snr = $json_sn[$key]['textinput']['comment'];
那么:

$snr_array = array()
foreach ($json_sn as $key)
    snr_array[] = $key['textinput']['comment'];
编辑:我刚刚意识到你可能只需要/得到一条评论:

$key = key($json_sn);
$snr = $json_sn[$key]['textinput']['comment'];
那么:

$snr_array = array()
foreach ($json_sn as $key)
    snr_array[] = $key['textinput']['comment'];
编辑:我刚刚意识到你可能只需要/得到一条评论:

$key = key($json_sn);
$snr = $json_sn[$key]['textinput']['comment'];

如果此格式始终相同,则可以在此格式上使用
reset()
函数。考虑这个例子:

$json_sn = json_decode($customer_json_sn, true);
$snr = reset($json_sn);
echo $snr['textinput']['comment'];

如果此格式始终相同,则可以在此格式上使用
reset()
函数。考虑这个例子:

$json_sn = json_decode($customer_json_sn, true);
$snr = reset($json_sn);
echo $snr['textinput']['comment'];

如果此格式始终相同,则可以在此格式上使用
reset()
函数。考虑这个例子:

$json_sn = json_decode($customer_json_sn, true);
$snr = reset($json_sn);
echo $snr['textinput']['comment'];

如果此格式始终相同,则可以在此格式上使用
reset()
函数。考虑这个例子:

$json_sn = json_decode($customer_json_sn, true);
$snr = reset($json_sn);
echo $snr['textinput']['comment'];
你可以做:

<?php

$json = '{"78":{"textinput":{"comment":"1"}},"82":"34"}';

var_dump(json_decode($json, true));

$data = json_decode($json, true);

foreach( $data as $key => $value ) {

    # Check if exsit
    if (isset($value['textinput']['comment'])) {
        echo "Value: " . $value['textinput']['comment'];
    }
}
您可以执行以下操作:

<?php

$json = '{"78":{"textinput":{"comment":"1"}},"82":"34"}';

var_dump(json_decode($json, true));

$data = json_decode($json, true);

foreach( $data as $key => $value ) {

    # Check if exsit
    if (isset($value['textinput']['comment'])) {
        echo "Value: " . $value['textinput']['comment'];
    }
}
您可以执行以下操作:

<?php

$json = '{"78":{"textinput":{"comment":"1"}},"82":"34"}';

var_dump(json_decode($json, true));

$data = json_decode($json, true);

foreach( $data as $key => $value ) {

    # Check if exsit
    if (isset($value['textinput']['comment'])) {
        echo "Value: " . $value['textinput']['comment'];
    }
}
您可以执行以下操作:

<?php

$json = '{"78":{"textinput":{"comment":"1"}},"82":"34"}';

var_dump(json_decode($json, true));

$data = json_decode($json, true);

foreach( $data as $key => $value ) {

    # Check if exsit
    if (isset($value['textinput']['comment'])) {
        echo "Value: " . $value['textinput']['comment'];
    }
}


为什么不使用
array\u keys
来获取键,或者对结果进行
array\u shift
来获取第一个键?您是否刚好在
注释
索引之后?您可以简单地使用
foreach
。它会给出结果。是的,我只需要注释为什么不使用
array\u keys
来获取键,或者对结果进行
array\u shift
来获取第一个键?您是否刚好在
注释
索引之后?您可以简单地使用
foreach
。它会给出结果。是的,我只需要注释为什么不使用
array\u keys
来获取键,或者对结果进行
array\u shift
来获取第一个键?您是否刚好在
注释
索引之后?您可以简单地使用
foreach
。它会给出结果。是的,我只需要注释为什么不使用
array\u keys
来获取键,或者对结果进行
array\u shift
来获取第一个键?您是否刚好在
注释
索引之后?您可以简单地使用
foreach
。它会给出结果。是的,我只需要注释,如果它不一样怎么办?@enigmaticus假设json结构保持不变,
reset()
将只接受第一个元素,因此,您不需要手动放置密钥,因为它总是会更改。如果它不相同,会发生什么情况?@enigmaticus假设json结构保持不变,
reset()
将只接受第一个元素,因此,您不需要手动放置密钥,因为它总是会更改。如果它不相同,会发生什么情况?@enigmaticus假设json结构保持不变,
reset()
将只接受第一个元素,因此,您不需要手动放置密钥,因为它总是会更改。如果它不相同,会发生什么情况?@enigmaticus假设json结构保持不变,
reset()
将只接受第一个元素,因此您不需要手动放置密钥,因为它总是会更改。