为了账号安全,请及时绑定邮箱和手机立即绑定

PHP数组获取值

PHP数组获取值

PHP
慕神8447489 2023-05-12 15:13:39
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}
查看完整描述

3 回答

?
慕码人8056858

TA贡献1803条经验 获得超6个赞

它是一个 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);


查看完整回答
反对 回复 2023-05-12
?
扬帆大鱼

TA贡献1799条经验 获得超9个赞

好吧,我会因为我的回答而被钉牢,因为我真的把它混为一谈,但它有效:


$result = "Xero 会计 API 的数组输出";


 $json = "";

 foreach ($result as $results) {

     $json = $json . $results;

 }

 $obj = json_decode($json);

  $Type = $obj->{'Type'};


查看完整回答
反对 回复 2023-05-12
?
qq_遁去的一_1

TA贡献1725条经验 获得超7个赞

You can deal with this JSON string in PHP as follows:

//声明一个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;


查看完整回答
反对 回复 2023-05-12
  • 3 回答
  • 0 关注
  • 133 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信