fork download
  1. <?php
  2. $in = print_r_reverse("
  3. Array
  4. (
  5. [0] => Array
  6. (
  7. [Import] => Array
  8. (
  9. [product_id] => 1
  10. [id] => 1
  11. [category_id] => 1
  12. [amount] => 50
  13. [cost] => 8320
  14. [paid] => 0
  15. [comment] => transportation and others cost: 100
  16. [created] => 2015-06-22 12:09:20
  17. )
  18.  
  19. [0] => Array
  20. (
  21. [total_sell] => 6
  22. )
  23.  
  24. )
  25.  
  26. [1] => Array
  27. (
  28. [Import] => Array
  29. (
  30. [product_id] => 2
  31. [id] => 2
  32. [category_id] => 2
  33. [amount] => 15
  34. [cost] => 3000
  35. [paid] => 0
  36. [comment] =>
  37. [created] => 2015-06-22 12:10:36
  38. )
  39.  
  40. [0] => Array
  41. (
  42. [total_sell] => 1
  43. )
  44.  
  45. )
  46.  
  47. [2] => Array
  48. (
  49. [Import] => Array
  50. (
  51. [product_id] => 1
  52. [id] => 3
  53. [category_id] => 1
  54. [amount] => 15
  55. [cost] => 2000
  56. [paid] => 0
  57. [comment] =>
  58. [created] => 2015-06-22 12:10:58
  59. )
  60.  
  61. [0] => Array
  62. (
  63. [total_sell] => 6
  64. )
  65.  
  66. )
  67.  
  68. [3] => Array
  69. (
  70. [Import] => Array
  71. (
  72. [product_id] => 1
  73. [id] => 4
  74. [category_id] => 1
  75. [amount] => 50
  76. [cost] => 8000
  77. [paid] => 0
  78. [comment] =>
  79. [created] => 2015-06-23 01:10:10
  80. )
  81.  
  82. [0] => Array
  83. (
  84. [total_sell] => 6
  85. )
  86.  
  87. )
  88.  
  89. )");
  90.  
  91.  
  92. $desired = print_r_reverse("
  93. Array
  94. (
  95. [0] => Array
  96. (
  97. [Import] => Array
  98. (
  99. [product_id] => 1
  100. [id] => 1
  101. [category_id] => 1
  102. [amount] => 50
  103. [cost] => 8320
  104. [paid] => 0
  105. [comment] => transportation and others cost: 100
  106. [created] => 2015-06-22 12:09:20
  107. )
  108.  
  109. [0] => Array
  110. (
  111. [total_sell] => 6
  112. )
  113.  
  114. )
  115.  
  116. [1] => Array
  117. (
  118. [Import] => Array
  119. (
  120. [product_id] => 2
  121. [id] => 2
  122. [category_id] => 2
  123. [amount] => 15
  124. [cost] => 3000
  125. [paid] => 0
  126. [comment] =>
  127. [created] => 2015-06-22 12:10:36
  128. )
  129.  
  130. [0] => Array
  131. (
  132. [total_sell] => 1
  133. )
  134.  
  135. )
  136.  
  137. )");
  138.  
  139.  
  140. ////////////////////
  141.  
  142. $out = [];
  143. $hashTable = [];
  144. foreach($in as $item) {
  145. $pid = $item['Import']['product_id'];
  146. if(!isset($hashTable[$pid])) {
  147. $out[] = $item;
  148. $hashTable[$pid] = true;
  149. }
  150. }
  151.  
  152. print_r($out);
  153.  
  154. // Prove they are the same!
  155. if( json_encode($out) === json_encode($desired) ) {
  156. echo "They are the same!!";
  157. }
  158.  
  159. //////////////////
  160.  
  161.  
  162. function print_r_reverse($in) {
  163. $lines = explode("\n", trim($in));
  164. if (trim($lines[0]) != 'Array') {
  165. // bottomed out to something that isn't an array
  166. return $in;
  167. } else {
  168. // this is an array, lets parse it
  169. if (preg_match("/(\s{5,})\(/", $lines[1], $match)) {
  170. // this is a tested array/recursive call to this function
  171. // take a set of spaces off the beginning
  172. $spaces = $match[1];
  173. $spaces_length = strlen($spaces);
  174. $lines_total = count($lines);
  175. for ($i = 0; $i < $lines_total; $i++) {
  176. if (substr($lines[$i], 0, $spaces_length) == $spaces) {
  177. $lines[$i] = substr($lines[$i], $spaces_length);
  178. }
  179. }
  180. }
  181. array_shift($lines); // Array
  182. array_shift($lines); // (
  183. array_pop($lines); // )
  184. $in = implode("\n", $lines);
  185. // make sure we only match stuff with 4 preceding spaces (stuff for this array and not a nested one)
  186. preg_match_all("/^\s{4}\[(.+?)\] \=\> /m", $in, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
  187. $pos = array();
  188. $previous_key = '';
  189. $in_length = strlen($in);
  190. // store the following in $pos:
  191. // array with key = key of the parsed array's item
  192. // value = array(start position in $in, $end position in $in)
  193. foreach ($matches as $match) {
  194. $key = $match[1][0];
  195. $start = $match[0][1] + strlen($match[0][0]);
  196. $pos[$key] = array($start, $in_length);
  197. if ($previous_key != '') $pos[$previous_key][1] = $match[0][1] - 1;
  198. $previous_key = $key;
  199. }
  200. $ret = array();
  201. foreach ($pos as $key => $where) {
  202. // recursively see if the parsed out value is an array too
  203. $ret[$key] = print_r_reverse(substr($in, $where[0], $where[1] - $where[0]));
  204. }
  205. return $ret;
  206. }
  207. }
Success #stdin #stdout 0.02s 24400KB
stdin
Standard input is empty
stdout
Array
(
    [0] => Array
        (
            [Import] => Array
                (
                    [product_id] => 1
                    [id] => 1
                    [category_id] => 1
                    [amount] => 50
                    [cost] => 8320
                    [paid] => 0
                    [comment] => transportation and others cost: 100  
                    [created] => 2015-06-22 12:09:20
                )

            [0] => Array
                (
                    [total_sell] => 6
                )

        )

    [1] => Array
        (
            [Import] => Array
                (
                    [product_id] => 2
                    [id] => 2
                    [category_id] => 2
                    [amount] => 15
                    [cost] => 3000
                    [paid] => 0
                    [comment] => 
                    [created] => 2015-06-22 12:10:36
                )

            [0] => Array
                (
                    [total_sell] => 1
                )

        )

)
They are the same!!