Ach ja. In PHP muss man in der Funktion json_decode() den zweiten Parameter "assoc" auf true
setzen, damit ein assoziativer Array (was man meist braucht!) herauskommt:
Also:
$array = json_decode( $json, true );
Vollständig:
<?php
$foo["id"] = "******";
$foo["created"] = "******";
$foo["storeAccountId"] = "1****9";
$foo["userIp"] = "*******";
$foo["transactionId"] = "EUW1-21";
$foo["transactionNumber"] = "LW6G8******";
$foo["currencyType"] = "EUR";
$foo["paymentType"] = "Sofort";
$foo["amount"] = 20.0;
$foo["success"] = true;
$foo["imported"] = false;
$bar[] = $foo;
$bar[] = $foo;
$s= json_encode ( $bar, JSON_PRETTY_PRINT );
echo "\n--------------------------------------------------\n";
echo '$s=' . $s;
echo "\n--------------------------------------------------\n";
$array = json_decode($s, true);
echo "\n--------------------------------------------------\n";
echo '$array='; print_r( $array);
echo "\n--------------------------------------------------\n";
echo json_last_error_msg();
echo "\n--------------------------------------------------\n";
$sum = 0;
foreach ( $array as $tupel ) {
$sum += $tupel['amount'];
}
echo "\n--------------------------------------------------\n";
echo 'Summe: ' . $sum . PHP_EOL;
Ausgaben:
php test.php
--------------------------------------------------
$s=[
{
"id": "******",
"created": "******",
"storeAccountId": "1****9",
"userIp": "*******",
"transactionId": "EUW1-21",
"transactionNumber": "LW6G8******",
"currencyType": "EUR",
"paymentType": "Sofort",
"amount": 20,
"success": true,
"imported": false
},
{
"id": "******",
"created": "******",
"storeAccountId": "1****9",
"userIp": "*******",
"transactionId": "EUW1-21",
"transactionNumber": "LW6G8******",
"currencyType": "EUR",
"paymentType": "Sofort",
"amount": 20,
"success": true,
"imported": false
}
]
--------------------------------------------------
--------------------------------------------------
$array=Array
(
[0] => Array
(
[id] => ******
[created] => ******
[storeAccountId] => 1****9
[userIp] => *******
[transactionId] => EUW1-21
[transactionNumber] => LW6G8******
[currencyType] => EUR
[paymentType] => Sofort
[amount] => 20
[success] => 1
[imported] =>
)
[1] => Array
(
[id] => ******
[created] => ******
[storeAccountId] => 1****9
[userIp] => *******
[transactionId] => EUW1-21
[transactionNumber] => LW6G8******
[currencyType] => EUR
[paymentType] => Sofort
[amount] => 20
[success] => 1
[imported] =>
)
)
--------------------------------------------------
No error
--------------------------------------------------
--------------------------------------------------
Summe: 40