fork(1) 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. // Stable sort
  143. sort($in);
  144.  
  145. // Reduce
  146. $out = array_reduce($in, function(&$acc, &$item){
  147. if($item['Import']['product_id'] !== @$acc[sizeof($acc)-1]['Import']['product_id']) {
  148. $acc[] = $item;
  149. }
  150. return $acc;
  151. }, []);
  152.  
  153.  
  154. print_r($out);
  155.  
  156. // Prove they are the same!
  157. if( json_encode($out) === json_encode($desired) ) {
  158. echo "They are the same!!";
  159. }
  160.  
  161. //////////////////
  162.  
  163.  
  164. function print_r_reverse($in) {
  165. $lines = explode("\n", trim($in));
  166. if (trim($lines[0]) != 'Array') {
  167. // bottomed out to something that isn't an array
  168. return $in;
  169. } else {
  170. // this is an array, lets parse it
  171. if (preg_match("/(\s{5,})\(/", $lines[1], $match)) {
  172. // this is a tested array/recursive call to this function
  173. // take a set of spaces off the beginning
  174. $spaces = $match[1];
  175. $spaces_length = strlen($spaces);
  176. $lines_total = count($lines);
  177. for ($i = 0; $i < $lines_total; $i++) {
  178. if (substr($lines[$i], 0, $spaces_length) == $spaces) {
  179. $lines[$i] = substr($lines[$i], $spaces_length);
  180. }
  181. }
  182. }
  183. array_shift($lines); // Array
  184. array_shift($lines); // (
  185. array_pop($lines); // )
  186. $in = implode("\n", $lines);
  187. // make sure we only match stuff with 4 preceding spaces (stuff for this array and not a nested one)
  188. preg_match_all("/^\s{4}\[(.+?)\] \=\> /m", $in, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
  189. $pos = array();
  190. $previous_key = '';
  191. $in_length = strlen($in);
  192. // store the following in $pos:
  193. // array with key = key of the parsed array's item
  194. // value = array(start position in $in, $end position in $in)
  195. foreach ($matches as $match) {
  196. $key = $match[1][0];
  197. $start = $match[0][1] + strlen($match[0][0]);
  198. $pos[$key] = array($start, $in_length);
  199. if ($previous_key != '') $pos[$previous_key][1] = $match[0][1] - 1;
  200. $previous_key = $key;
  201. }
  202. $ret = array();
  203. foreach ($pos as $key => $where) {
  204. // recursively see if the parsed out value is an array too
  205. $ret[$key] = print_r_reverse(substr($in, $where[0], $where[1] - $where[0]));
  206. }
  207. return $ret;
  208. }
  209. }
Success #stdin #stdout 0.02s 24448KB
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!!