En simpel måde at konvertere denne JSON til et PHP-Array: Erstat alle
": med
" => og alle
{ med
[, samt alle
} med
]Så skulle du have den korrekte php-kode, hvor du kan sætte dine værdier ind i.
$var = ["noget" => 1] er det samme som $var = array("noget" => 1), og skulle virke med mindre du kører en gammel php-version.
<?php
// Object til test:
$customer = (object) [
'customerNumber' => 1,
'currency' => 'DKK',
'name' => 'Test',
'address' => 'Testvej 1',
'zip' => '1234',
'city' => 'Testby',
'address' => 'Testvej 1',
'paymentTerms' => (object)['paymentTermsNumber' => 123],
'vatZone' => (object)['vatZoneNumber' => 123],
'delivery' => (object)[
"address" => "Hovedvejen 1",
"zip" => "2300",
"city" => "Kbh S",
"country" => "Denmark",
"deliveryDate" => "2014-09-14"
]
];
$order = [
"date" => date('Y-m-d'),
"currency" => $customer->currency,
"exchangeRate" => 100,
"netAmount" => 10.00,
"netAmountInBaseCurrency" => 0.00,
"grossAmount" => 12.50,
"marginInBaseCurrency" => -46.93,
"marginPercentage" => 0.0,
"vatAmount" => 2.50,
"roundingAmount" => 0.00,
"costPriceInBaseCurrency" => 46.93,
"paymentTerms" => [
"paymentTermsNumber" => $customer->paymentTerms->paymentTermsNumber,
"daysOfCredit" => 14,
"name" => "Lobende maned 14 dage",
"paymentTermsType" => "invoiceMonth"
],
"customer" => [
"customerNumber" => $customer->customerNumber
],
"recipient" => [
"name" => $customer->name,
"address" => $customer->address,
"zip" => $customer->zip,
"city" => $customer->city,
"vatZone" => [
"name" => "Domestic",
"vatZoneNumber" => $customer->vatZone->vatZoneNumber,
"enabledForCustomer" => true,
"enabledForSupplier" => true
]
],
"delivery" => [
"address" => $customer->delivery->address,
"zip" => $customer->delivery->zip,
"city" => $customer->delivery->city,
"country" => $customer->delivery->country,
"deliveryDate" => $customer->delivery->deliveryDate
],
"references" => [
"other" => "aaaa"
],
"layout" => [
"layoutNumber" => 5
],
];
$lines = [];
// Tilføj ny linje
$lines[] = [
"unit" => [
"unitNumber" => 2,
"name" => "Tim"
],
"product" => [
"productNumber" => "50"
],
"quantity" => 1.00,
"unitNetPrice" => 10.00,
"discountPercentage" => 0.00,
"unitCostPrice" => 46.93,
"totalNetAmount" => 10.00,
"marginInBaseCurrency" => -46.93,
"marginPercentage" => 0.0
];
// Tilføj ny linje
$lines[] = [
"unit" => [
"unitNumber" => 1,
"name" => "stk."
],
"product" => [
"productNumber" => "50"
],
"quantity" => 1.00,
"unitNetPrice" => 10.00,
"discountPercentage" => 0.00,
"unitCostPrice" => 46.93,
"totalNetAmount" => 10.00,
"marginInBaseCurrency" => -46.93,
"marginPercentage" => 0.0
];
// Tilføj ny linje
$lines[] = [
"unit" => [
"unitNumber" => 4
],
"product" => [
"productNumber" => "50"
],
"quantity" => 1.00,
"unitNetPrice" => 10.00,
"discountPercentage" => 0.00,
"unitCostPrice" => 46.93,
"totalNetAmount" => 10.00,
"marginInBaseCurrency" => -46.93,
"marginPercentage" => 0.0
];
// Indsæt alle linjer i order
array_push($order, $lines);
// Test indhold af $order
echo "<pre><br>";
echo var_export($order);
echo "<hr>";
echo json_encode($order);
echo "</pre><br>";
echo "https://jsonformatter.org/";