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.



Your result can be seen below.

Result of php executing





Full code of Insert meta to wallet.php

  1. <?php
  2.  
  3. $json = '[
  4.     {
  5.         "key":"Package A",
  6.         "value":"3"
  7.     },
  8.     {
  9.         "key":"Package B",
  10.         "value":null
  11.     },
  12.     {
  13.         "key":"Bonus",
  14.         "value":"50"
  15.     },
  16.     {
  17.         "key":"Etc.",
  18.         "value":"0"
  19.     },
  20.     {
  21.         "key":"Memo",
  22.         "value":"memo is here"
  23.     }
  24. ]';
  25.  
  26. $packageValue;
  27.  
  28. $walletMeta = json_decode($json, TRUE); # Decode as stdClass objects
  29.  
  30. //strip non-package info
  31. foreach($walletMeta as $k => $package){
  32.     if($package["key"] === "Bonus"){
  33.         unset($walletMeta[$k]);
  34.     }
  35.     else if($package["key"] === "Etc."){
  36.         unset($walletMeta[$k]);
  37.     }
  38.     else if($package["key"] === "Memo"){
  39.         unset($walletMeta[$k]);
  40.         break;
  41.     }
  42.     
  43.     continue;
  44. }
  45.  
  46.  
  47. //calculate credit
  48. $creditWalletMeta = $walletMeta;
  49. foreach($creditWalletMeta as $k => $package){
  50.     if($package["key"] == "Package A"){
  51.         $packageValue = 2; //this will be calculated based on packageValue if above 0
  52.         $creditWalletMeta[$k]['value']+=$packageValue;
  53.         break;
  54.     }
  55.     
  56.     continue;
  57. }
  58. echo print_r($walletMeta);
  59.  
  60. //calculate debit
  61. $debitWalletMeta = $walletMeta;
  62. //calculate credit
  63. foreach($debitWalletMeta as $k => $package){
  64.     if($package["key"] == "Package A"){
  65.         $packageValue = -2; //this will be calculated based on packageValue if above 0
  66.         $debitWalletMeta[$k]['value']+=$packageValue;
  67.         break;
  68.     }
  69.     
  70.     continue;
  71. }
  72.  
  73. echo print_r($debitWalletMeta);
File Description
  • Insert meta to wallet
  • PHP Code
  • 17 Feb-2021
  • 1.43 Kb
You can Share it: