Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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 我正在通过STOMP阅读JSON。有不同的JSON数据集,所以我需要计算出哪个JSON数据集已经通过。我通过阅读它的标题来做到这一点 然而,有一个特定的数据集我在阅读上有困难 foreach (json_decode($msg->body,true) as $event) { if(isset($event['schemaLocation'])) { $schedule_id=($event['schedule']['schedule_start

我正在尝试解码JSON

我正在通过STOMP阅读JSON。有不同的JSON数据集,所以我需要计算出哪个JSON数据集已经通过。我通过阅读它的标题来做到这一点

然而,有一个特定的数据集我在阅读上有困难

  foreach (json_decode($msg->body,true) as $event) {
    if(isset($event['schemaLocation'])) {
    $schedule_id=($event['schedule']['schedule_start_date']); 
    $signalling_id=($event['schedule']['schedule_segment']['signalling_id']);

    echo $schedule_id;
    }
在上面的示例中,isset函数工作正常,$schedule_id也获得了正确的答案

但是,$signaling\u id给出了一个未定义索引的错误:

这里是JSON的一部分转储(相当长………)。带有signaling_id的JSON片段位于JSON的末尾。非常感谢您为获取可变信号提供的任何帮助

    array(7) {
    ["schemaLocation"]=>
    string(72) "http://xml.networkrail.co.uk/ns/2008/Train itm_vstp_cif_messaging_v1.xsd"
    ["classification"]=>
    string(8) "industry"
    ["timestamp"]=>
    string(13) "1410374918000"
    ["owner"]=>
    string(12) "Network Rail"
    ["originMsgId"]=>
    string(47) "2014-09-10T18:48:38-00:00vstp.networkrail.co.uk"
    ["Sender"]=>
    array(3) {
      ["organisation"]=>
      string(12) "Network Rail"
      ["application"]=>
      string(4) "TOPS"
      ["component"]=>
      string(4) "VSTP"
    }
    ["schedule"]=>
    array(11) {
      ["schedule_id"]=>
      string(0) ""
      ["transaction_type"]=>
      string(6) "Create"
      ["schedule_start_date"]=>
      string(10) "2014-09-10"
      ["schedule_end_date"]=>
      string(10) "2014-09-10"
      ["schedule_days_runs"]=>
      string(7) "0010000"
      ["applicable_timetable"]=>
      string(1) "N"
      ["CIF_bank_holiday_running"]=>
      string(1) " "
      ["CIF_train_uid"]=>
      string(6) "W64017"
      ["train_status"]=>
      string(1) "1"
      ["CIF_stp_indicator"]=>
      string(1) "O"
      ["schedule_segment"]=>
      array(1) {
        [0]=>
        array(20) {
          ["signalling_id"]=>
          string(4) "5Y75"
          ["uic_code"]=>
          string(0) ""
          ["atoc_code"]=>
          string(0) ""
          ["CIF_train_category"]=>
          string(2) "EE"
          ["CIF_headcode"]=>
          string(0) ""
          ["CIF_course_indicator"]=
          ............................................ 

schedule_段本身是一个数组,因此

['schedule']['schedule_segment']['signalling_id']);
应该是这样的

['schedule']['schedule_segment'][0]['signalling_id']);

正如您在var转储中所看到的,信令_id位于另一个数组中。使用:

$signalling_id=($event ['schedule']['schedule_segment'][0]['signalling_id']);

如果键为0的一个元素数组在整个过程中不是常量,那么您可能需要一些逻辑来确定它在每个迭代中是什么。

现在就测试一下,谢谢。等待JSON通过STOMP