在json解码的数组中查找变量的索引


Finding the index of variable in a array decoded by json

我正在为我们的wordpress网站设置付费会员功能。
对于支付目的,我们尝试使用希腊支付处理器及其使用PHP的相应API调用。在某一点上我是成功的,但我被困在一个特定的点
提供的API函数之一是使用orderId作为引用来检索特定的订单详细信息。我的问题是处理返回的输出。
请求将被调用:

GET /api/transactions/?ordercode=175936509216

和样本返回将是RAW格式:

HTTP/1.1 200 OK
Connection: Keep-Alive
Content-Length: 1285
Expires: -1
Date: Wed, 16 Sep 2015 11:07:31 GMT
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
Cache-Control: no-cache
Pragma: no-cache
{ "ErrorCode" : 0,
  "ErrorText" : null,
  "TimeStamp" : "2015-09-16T14:07:31.4361544+03:00",
  "Transactions" : [ { "Amount" : 20.0,
        "ChannelId" : "d84f93ff-8ad0-4d97-a7a3-f187dd667553",
        "ClearanceDate" : "2015-09-10T01:10:50.71",
        "Commission" : 0.71999999999999997,
        "CreatedBy" : null,
        "CreditCard" : { "CardHolderName" : "Some name",
            "CardType" : { "CardTypeId" : 0,
                "Name" : "VISA"
              },
            "CountryCode" : "US",
            "IssuingBank" : "Demo Bank",
            "Number" : "411111XXXXXX1111",
            "Token" : "B6D3CC438671F16B211D83BBC077FEBD843CBC28"
          },
        "CurrentInstallment" : 0,
        "CustomerTrns" : null,
        "Fee" : 0.0,
        "InsDate" : "2015-09-09T16:15:09.42",
        "IsManualRefund" : false,
        "MerchantId" : "faae4194-7bf3-4b5c-9fc9-ec584397308c",
        "MerchantTrns" : null,
        "Order" : { "ChannelId" : "95d02b56-bace-4608-ad1b-c1a8da705e52",
            "OrderCode" : 491831172603,
            "RequestLang" : "en-US",
            "ResellerId" : null,
            "ResellerSourceCode" : null,
            "SourceCode" : "Default",
            "Tags" : [  ]
          },
        "ParentId" : null,
        "Payment" : { "ChannelId" : "d84f93ff-8ad0-4d97-a7a3-f187dd667553",
            "Email" : "some.email2@some.company.com",
            "FullName" : "Some names",
            "Installments" : 0,
            "Phone" : "6969696969",
            "RecurringSupport" : false
          },
        "ResellerId" : null,
        "ResellerSourceCode" : null,
        "SourceCode" : "Default",
        "StatusId" : "F",
        "TargetPersonId" : null,
        "TotalInstallments" : 0,
        "TransactionId" : "9b7af26e-bba7-4830-8d81-36dfd2bc3f3c",
        "TransactionType" : { "Name" : "ChargeCard",
            "TransactionTypeId" : 5
          }
      } ]
}

使用json解码函数转换为数组,并使用var_dumb函数,我们得到以下结果:

array(4) {
  ["Transactions"]=>
  array(1) {
    [0]=>
    array(24) {
      ["Fee"]=>
      float(0)
      ["ParentId"]=>
      NULL
      ["Amount"]=>
      float(20)
      ["StatusId"]=>
      string(1) "F"
      ["ChannelId"]=>
      string(36) "d84f93ff-8ad0-4d97-a7a3-f187dd667553"
      ["MerchantId"]=>
      string(36) "faae4194-7bf3-4b5c-9fc9-ec584397308c"
      ["ResellerId"]=>
      NULL
      ["InsDate"]=>
      string(22) "2015-09-09T16:15:09.42"
      ["CreatedBy"]=>
      NULL
      ["SourceCode"]=>
      string(7) "Default"
      ["TransactionId"]=>
      string(36) "9b7af26e-bba7-4830-8d81-36dfd2bc3f3c"
      ["Commission"]=>
      float(0.72)
      ["MerchantTrns"]=>
      NULL
      ["CustomerTrns"]=>
      NULL
      ["IsManualRefund"]=>
      bool(false)
      ["TargetPersonId"]=>
      NULL
      ["TotalInstallments"]=>
      int(0)
      ["CurrentInstallment"]=>
      int(0)
      ["ClearanceDate"]=>
      string(22) "2015-09-10T01:10:50.71"
      ["ResellerSourceCode"]=>
      NULL
      ["Order"]=>
      array(7) {
        ["OrderCode"]=>
        int(491831172603)
        ["ChannelId"]=>
        string(36) "95d02b56-bace-4608-ad1b-c1a8da705e52"
        ["ResellerId"]=>
        NULL
        ["SourceCode"]=>
        string(7) "Default"
        ["Tags"]=>
        array(0) {
        }
        ["RequestLang"]=>
        string(5) "en-US"
        ["ResellerSourceCode"]=>
        NULL
      }
      ["Payment"]=>
      array(6) {
        ["Email"]=>
        string(30) "somename@somecompany.com"
        ["Phone"]=>
        string(10) "6969696969"
        ["ChannelId"]=>
        string(36) "d84f93ff-8ad0-4d97-a7a3-f187dd667553"
        ["FullName"]=>
        string(17) "Some Name"
        ["Installments"]=>
        int(0)
        ["RecurringSupport"]=>
        bool(false)
      }
      ["TransactionType"]=>
      array(2) {
        ["Name"]=>
        string(10) "ChargeCard"
        ["TransactionTypeId"]=>
        int(5)
      }
      ["CreditCard"]=>
      array(6) {
        ["Token"]=>
        string(40) "B6D3CC438671F16B211D83BBC077FEBD843CBC28"
        ["Number"]=>
        string(16) "411111XXXXXX1111"
        ["CountryCode"]=>
        string(2) "US"
        ["IssuingBank"]=>
        string(9) "Demo Bank"
        ["CardHolderName"]=>
        string(17) "Some Name"
        ["CardType"]=>
        array(2) {
          ["Name"]=>
          string(4) "VISA"
          ["CardTypeId"]=>
          int(0)
        }
      }
    }
  }
  ["ErrorCode"]=>
  int(0)
  ["ErrorText"]=>
  NULL
  ["TimeStamp"]=>
  string(33) "2015-09-16T14:07:31.4361544+03:00"
}

问题是我如何找到这个数组中特定变量的具体索引是什么,以便可以分配给另一个新变量进行进一步处理。例如,如果我想在payment数组中获得email变量的内容,我该如何引用它呢?
目前,我工作的API是在演示开发人员部分,所以这里显示的数据只是虚拟数据使用print_r函数

添加请求的注释
    Array
    (
    [Transactions] => Array
    (
        [0] => Array
            (
                [Fee] => 0
                [ParentId] => 
                [Amount] => 20
                [StatusId] => F
                [ChannelId] => d84f93ff-8ad0-4d97-a7a3-f187dd667553
                [MerchantId] => faae4194-7bf3-4b5c-9fc9-ec584397308c
                [ResellerId] => 
                [InsDate] => 2015-09-09T16:15:09.42
                [CreatedBy] => 
                [SourceCode] => Default
                [TransactionId] => 9b7af26e-bba7-4830-8d81-36dfd2bc3f3c
                [Commission] => 0.72
                [MerchantTrns] => 
                [CustomerTrns] => 
                [IsManualRefund] => 
                [TargetPersonId] => 
                [TotalInstallments] => 0
                [CurrentInstallment] => 0
                [ClearanceDate] => 2015-09-10T01:10:50.71
                [ResellerSourceCode] => 
                [Order] => Array
                    (
                        [OrderCode] => 491831172603
                        [ChannelId] => 95d02b56-bace-4608-ad1b-c1a8da705e52
                        [ResellerId] => 
                        [SourceCode] => Default
                        [Tags] => Array
                            (
                            )
                        [RequestLang] => en-US
                        [ResellerSourceCode] => 
                    )
                [Payment] => Array
                    (
                        [Email] => someone@somecompany.com
                        [Phone] => 6969696969
                        [ChannelId] => d84f93ff-8ad0-4d97-a7a3-f187dd667553
                        [FullName] => Some Name
                        [Installments] => 0
                        [RecurringSupport] => 
                    )
                [TransactionType] => Array
                    (
                        [Name] => ChargeCard
                        [TransactionTypeId] => 5
                    )
                [CreditCard] => Array
                    (
                        [Token] => B6D3CC438671F16B211D83BBC077FEBD843CBC28
                        [Number] => 411111XXXXXX1111
                        [CountryCode] => US
                        [IssuingBank] => Demo Bank
                        [CardHolderName] => Leonidas leonidas
                        [CardType] => Array
                            (
                                [Name] => VISA
                                [CardTypeId] => 0
                            )
                    )
            )
    )
  [ErrorCode] => 0
  [ErrorText] => 
  [TimeStamp] => 2015-09-16T16:15:59.9665031+03:00
 )

看看这个。首先,将json解码为一个数组,在json_decode函数中使用第二个参数为true。

然后用foreach遍历它并检查transactions是否为数组,如果是则逐个遍历事务并按名称访问字段

  $myArray = json_decode('{"Transactions":[{"Fee":0.00,"ParentId":null,"Amount":20.00,"StatusId":"F","ChannelId":"d84f93ff-8ad0-4d97-a7a3-f187dd667553","MerchantId":"faae4194-7bf3-4b5c-9fc9-ec584397308c","ResellerId":null,"InsDate":"2015-09-09T16:15:09.42","CreatedBy":null,"SourceCode":"Default","TransactionId":"9b7af26e-bba7-4830-8d81-36dfd2bc3f3c","Commission":0.72,"MerchantTrns":null,"CustomerTrns":null,"IsManualRefund":false,"TargetPersonId":null,"TotalInstallments":0,"CurrentInstallment":0,"ClearanceDate":"2015-09-10T01:10:50.71","ResellerSourceCode":null,"Order":{"OrderCode":491831172603,"ChannelId":"95d02b56-bace-4608-ad1b-c1a8da705e52","ResellerId":null,"SourceCode":"Default","Tags":[],"RequestLang":"en-US","ResellerSourceCode":null},"Payment":{"Email":"some.email2@some.company.com","Phone":"6969696969","ChannelId":"d84f93ff-8ad0-4d97-a7a3-f187dd667553","FullName":"Some names","Installments":0,"RecurringSupport":false},"TransactionType":{"Name":"ChargeCard","TransactionTypeId":5},"CreditCard":{"Token":"B6D3CC438671F16B211D83BBC077FEBD843CBC28","Number":"411111XXXXXX1111","CountryCode":"US","IssuingBank":"Demo Bank","CardHolderName":"Some name","CardType":{"Name":"VISA","CardTypeId":0}}}],"ErrorCode":0,"ErrorText":null,"TimeStamp":"2015-09-16T14:07:31.4361544+03:00"}',true);
  foreach($myArray as $transactions){   
        if(is_array($transactions))
            foreach($transactions as $transaction){
            echo "<pre>";
                print_r($transaction);
            echo "</pre>";
            echo "<br>";
            if($transaction['Payment']){
                echo $transaction['Payment']['Email'];
            }
        }
    }

输出为some.email2@some.company.com欢呼声