Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. #--------------------------------------------------------#
  2. # Miva Data To PHP Array
  3. #--------------------------------------------------------#
  4. # @author: Patrick Stearns <pstearns@miva.com> - 2015
  5. #--------------------------------------------------------#
  6. # Converts a serialized Miva structure into a PHP multi-
  7. # dimensional associative array
  8. #
  9. # @param string, $mv_serialized_str:
  10. # - Miva data converted to serialized string
  11. # Example input str: ':email=pstearns%2B1@miva.com,:gross_amount=1085.43,
  12. # :net_amount=989.00,:order_date=09%2F23%2F2015,
  13. # :order_id=1018,:order_time=14%3A11%3A36+PST'
  14. #
  15. # @return array, $result:
  16. # - Converted multi-dimenional associative array
  17. #--------------------------------------------------------#
  18. function mvDataToArray($mv_serialized_str)
  19. {
  20. $result = [];
  21.  
  22. // Split the comma separated list into an array
  23. $csl_arr = explode(',', $mv_serialized_str);
  24.  
  25. // Loop through the items that were formerly comma separated
  26. foreach ($csl_arr as $item) {
  27. // These values are for checking if the array key is Miva array key. Reset these to null
  28. // for each loop iteration.
  29. $mv_array_key = null;
  30. $mv_array_index = null;
  31.  
  32. // The first ':' is always deadweight, we can go ahead and trim it off
  33. $item = ltrim($item, ':');
  34.  
  35. // Split each item by the ':' delimiter and store it into a new array
  36. $col_sep_arr = explode(':', $item);
  37.  
  38. // If there is only one item left in the colon separated array, we can go ahead and
  39. // map it to the final result
  40. if (count($col_sep_arr) === 1) {
  41. // Split the item by the '=' delimiter ([0] = key, [1] = value)
  42. $target_arr = explode('=', $col_sep_arr[0]);
  43.  
  44. // Check if the key is a Miva array
  45. preg_match('/(\w+)?\[(\d+)\]/', $target_arr[0], $match);
  46.  
  47. // The key is a Miva array
  48. if ($match) {
  49. // Store the Miva array key name
  50. $mv_array_key = $match[1];
  51.  
  52. // Store the Miva array index value and ensure the value is an int
  53. $mv_array_index = (int)$match[2];
  54.  
  55. // The key has a name and index (Ex: example[1]).
  56. if ($mv_array_key && $mv_array_index) {
  57. // Decode url-encoding and set the value of the array element
  58. $result[$mv_array_key][$mv_array_index] = urldecode($target_arr[1]);
  59.  
  60. continue;
  61. }
  62. // The key has an index only (Ex: [1]).
  63. else if (!$mv_array_key && $mv_array_index) {
  64. // Decode url-encoding and set the value of the array element
  65. $result[$mv_array_index] = urldecode($target_arr[1]);
  66.  
  67. continue;
  68. }
  69. }
  70.  
  71. // Decode url-encoding and set the value of the array element
  72. $result[$target_arr[0]] = urldecode($target_arr[1]);
  73. }
  74. // Else there are multiple items left in the colon separated array
  75. // ...and so it begins...
  76. else {
  77. // Our target is the last item in the colon separated array, let's pop it off
  78. // of the array
  79. $target_item = array_pop($col_sep_arr);
  80.  
  81. // Time to break up the party. Split the target item by the '=' delimiter.
  82. $target_arr = explode('=', $target_item);
  83.  
  84. // Decode the value's url-encoding
  85. $target_val = urldecode($target_arr[1]);
  86.  
  87. // Push the target item's key back onto the colon separated array
  88. array_push($col_sep_arr, $target_arr[0]);
  89.  
  90. // Now for the fun part...
  91. // Create a reference of the results array to keep track of its current dimension
  92. $result_ref = &$result;
  93.  
  94. // Loop through the colon separated array, which should now only contain the keys
  95. // to map to the proper dimension of the results array
  96. while ($key = array_shift($col_sep_arr)) {
  97. // Check if the key is a Miva array
  98. preg_match('/(\w+)?\[(\d+)\]/', $key, $match);
  99.  
  100. // The key is a Miva array
  101. if ($match) {
  102. // Store the Miva array key name
  103. $mv_array_key = $match[1];
  104.  
  105. // Store the Miva array index value and ensure the value is an int
  106. $mv_array_index = (int)$match[2];
  107.  
  108. // The key has a name and index (Ex: example[1])
  109. if ($mv_array_key && $mv_array_index) {
  110. // Rename the key to the Miva array key minus the index
  111. $key = $mv_array_key;
  112.  
  113. // If the key doesn't already exist in the results array, add it as a layer
  114. // by setting the key to an empty array and the array index that follows it
  115. if (!isset($result_ref[$key])) {
  116. $result_ref[$key] = [];
  117. $result_ref[$key][$mv_array_index] = [];
  118. }
  119.  
  120. // Move the reference index deeper and continue to the next key
  121. $result_ref = &$result_ref[$key][$mv_array_index];
  122.  
  123. continue;
  124. }
  125. // The key has an index only (Ex: [1]). Store this value to keep track of
  126. // which index we are at in the array
  127. else if (!$mv_array_key && $mv_array_index) {
  128. // Move the reference to point to the index and continue to the next key
  129. $result_ref = &$result_ref[$mv_array_index];
  130.  
  131. continue;
  132. }
  133. }
  134.  
  135. // If the key doesn't already exist in the results array, add it as a layer
  136. // by setting the key to an empty array
  137. if (!isset($result_ref[$key])) {
  138. $result_ref[$key] = [];
  139. }
  140.  
  141. // Move the reference deeper...
  142. $result_ref = &$result_ref[$key];
  143. }
  144.  
  145. // Now that we have our dimensions set, assign value of this dimension to the
  146. // target value that we stored earlier
  147. $result_ref = $target_val;
  148. }
  149. }
  150.  
  151. // ...and voila, return the results
  152. return $result;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement