PHP数组获取值

PHP数组获取值,php,arrays,Php,Arrays,N00B在这里,我有以下php数组输出,在我的一生中,我无法提取数组值-我已经在数组中循环并尝试输出$array['AmountPaid']。我注意到在其他帖子中,数组有一个=>而不是:来分隔键和值,然后使用:$array=>AmountPaid { "Type": "ACCREC", "Contact": { "ContactID": "37918a06-92f6-4edb-bfe0-1fc041c90f8b", "Name": "Boom FM", "Cont

N00B在这里,我有以下php数组输出,在我的一生中,我无法提取数组值-我已经在数组中循环并尝试输出
$array['AmountPaid']
。我注意到在其他帖子中,数组有一个=>而不是:来分隔键和值,然后使用:$array=>AmountPaid

{
  "Type": "ACCREC",
  "Contact": {
    "ContactID": "37918a06-92f6-4edb-bfe0-1fc041c90f8b",
    "Name": "Boom FM",
    "ContactPersons": [],
    "Addresses": [],
    "Phones": [],
    "ContactGroups": [],
    "HasAttachments": false,
    "HasValidationErrors": false
  },
  "LineItems": [],
  "Date": "2020-04-08",
  "DueDate": "2020-04-21",
  "LineAmountTypes": "Exclusive",
  "InvoiceNumber": "INV-0010",
  "Reference": "Training",
  "CurrencyCode": "USD",
  "Status": "PAID",
  "SubTotal": 500,
  "TotalTax": 41.25,
  "Total": 541.25,
  "InvoiceID": "4c4db294-3633-45cd-8706-f0b3b0079609",
  "HasAttachments": false,
  "IsDiscounted": false,
  "Payments": [],
  "Prepayments": [],
  "Overpayments": [],
  "AmountDue": 0,
  "AmountPaid": 0,
  "FullyPaidOnDate": "2020-04-19",
  "AmountCredited": 541.25,
  "UpdatedDateUTC": "2008-12-20T18:38:32+01:00",
  "CreditNotes": [
    {
      "Date": "2020-04-19",
      "LineItems": [],
      "Total": 541.25,
      "CreditNoteID": "602f5486-664e-492f-b4d1-e12df1d4b8ba",
      "CreditNoteNumber": "CN-0014",
      "AppliedAmount": 541.25,
      "HasAttachments": false,
      "HasErrors": false
    }
  ],
  "HasErrors": false
}

它是一个JSON字符串,而不是PHP数组。要转换,可以执行以下操作:

    $string = '{ "Type": "ACCREC", "Contact": { "ContactID": "37918a06-92f6-4edb-bfe0-1fc041c90f8b", "Name": "Boom FM", "ContactPersons": [], "Addresses": [], "Phones": [], "ContactGroups": [], "HasAttachments": false, "HasValidationErrors": false }, "LineItems": [], "Date": "2020-04-08", "DueDate": "2020-04-21", "LineAmountTypes": "Exclusive", "InvoiceNumber": "INV-0010", "Reference": "Training", "CurrencyCode": "USD", "Status": "PAID", "SubTotal": 500, "TotalTax": 41.25, "Total": 541.25, "InvoiceID": "4c4db294-3633-45cd-8706-f0b3b0079609", "HasAttachments": false, "IsDiscounted": false, "Payments": [], "Prepayments": [], "Overpayments": [], "AmountDue": 0, "AmountPaid": 0, "FullyPaidOnDate": "2020-04-19", "AmountCredited": 541.25, "UpdatedDateUTC": "2008-12-20T18:38:32+01:00", "CreditNotes": [ { "Date": "2020-04-19", "LineItems": [], "Total": 541.25, "CreditNoteID": "602f5486-664e-492f-b4d1-e12df1d4b8ba", "CreditNoteNumber": "CN-0014", "AppliedAmount": 541.25, "HasAttachments": false, "HasErrors": false } ], "HasErrors": false }'

    $data = json_decode($string, true); //Second parameter forces conversion to array

    var_dump($data);
//声明一个json字符串

$json = '{ "Type": "ACCREC", "Contact": { "ContactID": "37918a06-92f6-4edb-bfe0-1fc041c90f8b", "Name": "Boom FM", "ContactPersons": [], "Addresses": [], "Phones": [], "ContactGroups": [], "HasAttachments": false, "HasValidationErrors": false }, "LineItems": [], "Date": "2020-04-08", "DueDate": "2020-04-21", "LineAmountTypes": "Exclusive", "InvoiceNumber": "INV-0010", "Reference": "Training", "CurrencyCode": "USD", "Status": "PAID", "SubTotal": 500, "TotalTax": 41.25, "Total": 541.25, "InvoiceID": "4c4db294-3633-45cd-8706-f0b3b0079609", "HasAttachments": false, "IsDiscounted": false, "Payments": [], "Prepayments": [], "Overpayments": [], "AmountDue": 0, "AmountPaid": 0, "FullyPaidOnDate": "2020-04-19", "AmountCredited": 541.25, "UpdatedDateUTC": "2008-12-20T18:38:32+01:00", "CreditNotes": [ { "Date": "2020-04-19", "LineItems": [], "Total": 541.25, "CreditNoteID": "602f5486-664e-492f-b4d1-e12df1d4b8ba", "CreditNoteNumber": "CN-0014", "AppliedAmount": 541.25, "HasAttachments": false, "HasErrors": false } ], "HasErrors": false }'; 



//1st Way: 

 // Use json_decode() function to 
$obj= json_decode($json);
$item = $obj->{'Type'};
print $item;


//2nd way:
$obj= json_decode($json,true);
$item = $obj['Type'];
print $item;

好吧,我会被我的答案钉死的,因为我真的很讨厌这个,但它是有效的:

$result=“Xero Accounting API的数组输出”


这给了我警告:json_decode()期望参数1是字符串,所以我使用$result=json_decode(json_encode($result),true);相反,这消除了错误。如果我使用foreach循环和$result=>AmountPaid来获取值仍然不起作用,
json\u decode
json\u encode
是互补函数,因此
$result=json\u decode(json\u encode($result),true)将回到您开始的地方。上面发布的我的代码有效。您提到的错误消息表明您实际上不是以字符串开头的。当你说
var\u dump($result)时,你看到了什么
在使用
json\u encode
之前,数组(1){[0]=>object(XeroAPI\xerohp\Models\Accounting\Invoice){79(1){[“container”:protected]=>array(38){[“type”]=>string(6)“ACCREC”[“contact”]=>object(XeroAPI\xerohp\Models\Accounting\contact\contact){72(1){[“container”:protected]=>数组(39){[“contact\u id”=>string(36)“3cbd5263-0965-4c4e-932c-bf50e3297610”[“联系人号码”]=>NULL[“帐户号码”]=>NULL[“联系人状态”]=>NULL[“姓名”]=>string(15)“Rex媒体组”[“名字”]=>NULL[“姓氏”]=>NULL[“电子邮件地址”=>NULL[“skype\u用户名”]=>NULL[“联系人”=>array(0)。。。。。。。
$json = '{ "Type": "ACCREC", "Contact": { "ContactID": "37918a06-92f6-4edb-bfe0-1fc041c90f8b", "Name": "Boom FM", "ContactPersons": [], "Addresses": [], "Phones": [], "ContactGroups": [], "HasAttachments": false, "HasValidationErrors": false }, "LineItems": [], "Date": "2020-04-08", "DueDate": "2020-04-21", "LineAmountTypes": "Exclusive", "InvoiceNumber": "INV-0010", "Reference": "Training", "CurrencyCode": "USD", "Status": "PAID", "SubTotal": 500, "TotalTax": 41.25, "Total": 541.25, "InvoiceID": "4c4db294-3633-45cd-8706-f0b3b0079609", "HasAttachments": false, "IsDiscounted": false, "Payments": [], "Prepayments": [], "Overpayments": [], "AmountDue": 0, "AmountPaid": 0, "FullyPaidOnDate": "2020-04-19", "AmountCredited": 541.25, "UpdatedDateUTC": "2008-12-20T18:38:32+01:00", "CreditNotes": [ { "Date": "2020-04-19", "LineItems": [], "Total": 541.25, "CreditNoteID": "602f5486-664e-492f-b4d1-e12df1d4b8ba", "CreditNoteNumber": "CN-0014", "AppliedAmount": 541.25, "HasAttachments": false, "HasErrors": false } ], "HasErrors": false }'; 



//1st Way: 

 // Use json_decode() function to 
$obj= json_decode($json);
$item = $obj->{'Type'};
print $item;


//2nd way:
$obj= json_decode($json,true);
$item = $obj['Type'];
print $item;
 $json = "";
 foreach ($result as $results) {
     $json = $json . $results;
 }
 $obj = json_decode($json);
  $Type = $obj->{'Type'};