JSON DB Daten extrahieren
bearbeitet von ursus contionabundoAch ja. [In PHP muss man in der Funktion json_decode() den zweiten Parameter "assoc" auf `true` setzen](http://php.net/manual/de/function.json-decode.php), damit ein assoziativer Array (was an meist braucht!) herauskommt:
Also:
~~~php
$array = json_decode( $json, true );
~~~
Vollständig:
~~~php
<?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
~~~
JSON DB Daten extrahieren
bearbeitet von ursus contionabundoAch ja. [In PHP muss man in der Funktion json_decode() den zweiten Parameter "assoc" auf `true` setzen](http://php.net/manual/de/function.json-decode.php), damit ein assoziativer Array (was an meist braucht!) herauskommt:
Also:
~~~php
$array = json_decode( $json, true );
~~~
Vollständig:
~~~php
<?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;
~~~
JSON DB Daten extrahieren
bearbeitet von ursus contionabundoAch ja. [In PHP muss man in der Funktion json_decode() den zweiten Parameter "assoc" auf `true` setzen](http://php.net/manual/de/function.json-decode.php), damit ein assoziativer Array (was an meist braucht!) herauskommt:
Also:
~~~php
$array = json_decode( $json, true );
~~~
Vollständig:
~~~php
<?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 '$s=' . $s;
$array = json_decode($s, true);
echo "\n--------------------------------------------------\n";
echo '$array='; print_r( $array);
echo "\n--------------------------------------------------\n";
echo json_last_error_msg() . PHP_EOL;
echo "\n--------------------------------------------------\n";
$sum = 0;
foreach ( $array as $tupel ) {
    $sum += $tupel['amount'];
}
echo "\n--------------------------------------------------\n";
echo 'Summe: ' . $sum . PHP_EOL;
~~~
JSON DB Daten extrahieren
bearbeitet von ursus contionabundoAch ja. In PHP muss man assoc auf true setzen, damit ein assoziativer Array (was an meist braucht!) herauskommt:
Also:
~~~php
$array = json_decode( $hson, true );
~~~
Vollständig:
~~~php
<?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 '$s=' . $s;
$array = json_decode($s, true);
echo "\n--------------------------------------------------\n";
echo '$array='; print_r( $array);
echo "\n--------------------------------------------------\n";
echo json_last_error_msg() . PHP_EOL;
echo "\n--------------------------------------------------\n";
$sum = 0;
foreach ( $array as $tupel ) {
    $sum += $tupel['amount'];
}
echo "\n--------------------------------------------------\n";
echo 'Summe: ' . $sum . PHP_EOL;
~~~