简体   繁体   中英

PHP array get values

N00B here, I have the following php array output, and for the life of me I cannot extract the array values - I have looped through the array and tried to output $array['AmountPaid'] . I have noticed in other postings the array has a => rather than: to separate the key from the values and then this is used: $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
}

It's a JSON string and not a PHP array. To convert, you can do this:

    $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);
You can deal with this JSON string in PHP as follows:

// Declare a json string

$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;

Ok, Im going to get nailed for my answer because ive really bastardized this but it works:

$result = "Array output from Xero Accounting API";

 $json = "";
 foreach ($result as $results) {
     $json = $json . $results;
 }
 $obj = json_decode($json);
  $Type = $obj->{'Type'};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM