Insert meta to wallet - PHP Online
Form of PHP Sandbox
Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php
in the places where the PHP script should be executed.
Result of php executing
Full code of Insert meta to wallet.php
- <?php
- $json = '[
- {
- "key":"Package A",
- "value":"3"
- },
- {
- "key":"Package B",
- "value":null
- },
- {
- "key":"Bonus",
- "value":"50"
- },
- {
- "key":"Etc.",
- "value":"0"
- },
- {
- "key":"Memo",
- "value":"memo is here"
- }
- ]';
- $packageValue;
- $walletMeta = json_decode($json, TRUE); # Decode as stdClass objects
- //strip non-package info
- foreach($walletMeta as $k => $package){
- if($package["key"] === "Bonus"){
- unset($walletMeta[$k]);
- }
- else if($package["key"] === "Etc."){
- unset($walletMeta[$k]);
- }
- else if($package["key"] === "Memo"){
- unset($walletMeta[$k]);
- break;
- }
- continue;
- }
- //calculate credit
- $creditWalletMeta = $walletMeta;
- foreach($creditWalletMeta as $k => $package){
- if($package["key"] == "Package A"){
- $packageValue = 2; //this will be calculated based on packageValue if above 0
- $creditWalletMeta[$k]['value']+=$packageValue;
- break;
- }
- continue;
- }
- echo print_r($walletMeta);
- //calculate debit
- $debitWalletMeta = $walletMeta;
- //calculate credit
- foreach($debitWalletMeta as $k => $package){
- if($package["key"] == "Package A"){
- $packageValue = -2; //this will be calculated based on packageValue if above 0
- $debitWalletMeta[$k]['value']+=$packageValue;
- break;
- }
- continue;
- }
- echo print_r($debitWalletMeta);