fork(1) download
  1. <?php
  2.  
  3. // ORIGINAL ARRAY STRING FROM POST
  4. $string_of_array = 'Array
  5. (
  6. [0] => Array
  7. (
  8. [ID] => 1
  9. [UserInfo] => Array
  10. (
  11. [ID] => 1
  12. [caps] => Array
  13. (
  14. [administrator] => 1
  15. )
  16.  
  17. [cap_key] => wp_capabilities
  18. [roles] => Array
  19. (
  20. [0] => administrator
  21. )
  22.  
  23. [allcaps] => Array
  24. (
  25. [switch_themes] => 1
  26. [edit_themes] => 1
  27. [activate_plugins] => 1
  28. [edit_plugins] => 1
  29. [edit_users] => 1
  30. [edit_files] => 1
  31. [manage_options] => 1
  32. [moderate_comments] => 1
  33. [manage_categories] => 1
  34. [manage_links] => 1
  35. [upload_files] => 1
  36. [import] => 1
  37. [unfiltered_html] => 1
  38. [edit_posts] => 1
  39. [edit_others_posts] => 1
  40. [edit_published_posts] => 1
  41. [publish_posts] => 1
  42. [edit_pages] => 1
  43. [read] => 1
  44. [level_10] => 1
  45. [level_9] => 1
  46. [level_8] => 1
  47. [level_7] => 1
  48. [level_6] => 1
  49. [level_5] => 1
  50. [level_4] => 1
  51. [level_3] => 1
  52. [level_2] => 1
  53. [level_1] => 1
  54. [level_0] => 1
  55. [edit_others_pages] => 1
  56. [edit_published_pages] => 1
  57. [publish_pages] => 1
  58. [delete_pages] => 1
  59. [delete_others_pages] => 1
  60. [delete_published_pages] => 1
  61. [delete_posts] => 1
  62. [delete_others_posts] => 1
  63. [delete_published_posts] => 1
  64. [delete_private_posts] => 1
  65. [edit_private_posts] => 1
  66. [read_private_posts] => 1
  67. [delete_private_pages] => 1
  68. [edit_private_pages] => 1
  69. [read_private_pages] => 1
  70. [delete_users] => 1
  71. [create_users] => 1
  72. [unfiltered_upload] => 1
  73. [edit_dashboard] => 1
  74. [update_plugins] => 1
  75. [delete_plugins] => 1
  76. [install_plugins] => 1
  77. [update_themes] => 1
  78. [install_themes] => 1
  79. [update_core] => 1
  80. [list_users] => 1
  81. [remove_users] => 1
  82. [add_users] => 1
  83. [promote_users] => 1
  84. [edit_theme_options] => 1
  85. [delete_themes] => 1
  86. [export] => 1
  87. [administrator] => 1
  88. )
  89.  
  90. [filter] =>
  91. [user_login] => admin
  92. [user_nicename] => admin
  93. [user_email] => goranefbl@gmail.com
  94. [user_url] =>
  95. [user_registered] => 2014-01-29 10:57:09
  96. [user_activation_key] =>
  97. [user_status] => 0
  98. [display_name] => admin
  99. [wlm_feed_url] => http://p...content-available-to-author-only...n.com/excel/feed/?wpmfeedkey=1;2e7e48ca65d94e5f0ec1baae46e4972c
  100. [wpm_login_date] => 1392155735
  101. [wpm_login_ip] => 62.68.119.252
  102. )
  103.  
  104. [Sequential] =>
  105. [Levels] => Array
  106. (
  107. [1391447566] => stdClass Object
  108. (
  109. [Level_ID] => 1391447566
  110. [Name] => Team Membership
  111. [Cancelled] =>
  112. [CancelDate] =>
  113. [Pending] =>
  114. [UnConfirmed] =>
  115. [Expired] =>
  116. [ExpiryDate] => 1393866766
  117. [SequentialCancelled] =>
  118. [Active] => 1
  119. [Status] => Array
  120. (
  121. [0] => Active
  122. )
  123.  
  124. [Timestamp] => 1391447566
  125. [TxnID] => WL-1-1391447566
  126. )
  127.  
  128. )
  129.  
  130. [PayPerPosts] => Array
  131. (
  132. )
  133.  
  134. )
  135.  
  136. )';
  137.  
  138.  
  139.  
  140. // CONVERT THE STRING TO AN ARRAY
  141. $response = print_r_reverse($string_of_array);
  142.  
  143.  
  144.  
  145. // RECREATE THE OBJECT PART OF THE ARRAY
  146. $object = new stdClass();
  147. $object->Level_ID = 1391447566;
  148. $object->Name = 'Team Membership';
  149. $object->Cancelled = '';
  150. $object->CancelDate = '';
  151. $object->Pending = '';
  152. $object->UnConfirmed = '';
  153. $object->Expired = '';
  154. $object->ExpiryDate = 1393866766;
  155. $object->SequentialCancelled = '';
  156. $object->Active = 1;
  157. $object->Status = array('Active');
  158. $object->Timestamp = 1391447566;
  159. $object->TxnID = 'WL-1-1391447566';
  160.  
  161.  
  162.  
  163. // ADD THE OBJECT TO THE ARRAY
  164. $response[0]['Levels'][] = $object;
  165.  
  166.  
  167.  
  168. // UNSET THE ORIGINAL MOCK OBJECT SINCE IT'S NOT A REAL OBJECT
  169. unset($response[0]['Levels']['1391447566']);
  170.  
  171.  
  172. // PRINT THE FULL ARRAY TO MAKE SURE WE ARE ON TRACK
  173. print "\n\n<PRE><FONT COLOR=ORANGE>"; print_r($response); print "</FONT></PRE>";
  174.  
  175.  
  176. // LOOK AT THE LEVELS PART OF THE ARRAY SPECIFICALLY
  177. print "\n\n<PRE><FONT COLOR=RED>"; print_r($response[0]['Levels']); print "</FONT></PRE>";
  178.  
  179.  
  180. // LOOP THROUGH EACH LEVEL AND PULL OUT THE NAME
  181. foreach ($response[0]['Levels'] AS $level_key => $level_val) {
  182.  
  183.  
  184. // PRINT OUT DEBUG STUFF
  185. print "\n<BR><FONT COLOR=GREEN>KEY:</FONT> <FONT COLOR=BLUE>".$level_key."</FONT>";
  186. print "\n<BR><FONT COLOR=GREEN>VAL:</FONT> <FONT COLOR=BLUE><PRE>".print_r($level_val, true)."</PRE></FONT>";
  187. print "\n<BR><FONT COLOR=GREEN>NAME:</FONT> <FONT COLOR=BLUE>".$level_val->Name."</FONT>";
  188.  
  189.  
  190. // ASSIGN THE LEVEL NAME TO A VARIABLE
  191. $level_name = $level_val->Name;
  192.  
  193. }
  194.  
  195.  
  196. // PRINT THE OUTPUT
  197. echo "\n\n<BR><BR>Final Captured Level Name: ".$level_name;
  198.  
  199.  
  200.  
  201. // FUNCTION TO TURN THE 'print_r' VERSION OF THE ARRAY INTO AN ACTUAL ARRAY
  202. function print_r_reverse($in) {
  203. $lines = explode("\n", trim($in));
  204. if (trim($lines[0]) != 'Array') {
  205. // bottomed out to something that isn't an array
  206. return $in;
  207. } else {
  208. // this is an array, lets parse it
  209. if (preg_match("/(\s{5,})\(/", $lines[1], $match)) {
  210. // this is a tested array/recursive call to this function
  211. // take a set of spaces off the beginning
  212. $spaces = $match[1];
  213. $spaces_length = strlen($spaces);
  214. $lines_total = count($lines);
  215. for ($i = 0; $i < $lines_total; $i++) {
  216. if (substr($lines[$i], 0, $spaces_length) == $spaces) {
  217. $lines[$i] = substr($lines[$i], $spaces_length);
  218. }
  219. }
  220. }
  221. array_shift($lines); // Array
  222. array_shift($lines); // (
  223. array_pop($lines); // )
  224. $in = implode("\n", $lines);
  225. // make sure we only match stuff with 4 preceding spaces (stuff for this array and not a nested one)
  226. preg_match_all("/^\s{4}\[(.+?)\] \=\> /m", $in, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
  227. $pos = array();
  228. $previous_key = '';
  229. $in_length = strlen($in);
  230. // store the following in $pos:
  231. // array with key = key of the parsed array's item
  232. // value = array(start position in $in, $end position in $in)
  233. foreach ($matches as $match) {
  234. $key = $match[1][0];
  235. $start = $match[0][1] + strlen($match[0][0]);
  236. $pos[$key] = array($start, $in_length);
  237. if ($previous_key != '') $pos[$previous_key][1] = $match[0][1] - 1;
  238. $previous_key = $key;
  239. }
  240. $ret = array();
  241. foreach ($pos as $key => $where) {
  242. // recursively see if the parsed out value is an array too
  243. $ret[$key] = print_r_reverse(substr($in, $where[0], $where[1] - $where[0]));
  244. }
  245. return $ret;
  246. }
  247. }
Success #stdin #stdout 0.03s 20520KB
stdin
Standard input is empty
stdout

<PRE><FONT COLOR=ORANGE>Array
(
    [0] => Array
        (
            [ID] => 1
            [UserInfo] => Array
                (
                    [ID] => 1
                    [caps] => Array
                        (
                            [administrator] => 1
                        )

                    [cap_key] => wp_capabilities
                    [roles] => Array
                        (
                            [0] => administrator
                        )

                    [allcaps] => Array
                        (
                            [switch_themes] => 1
                            [edit_themes] => 1
                            [activate_plugins] => 1
                            [edit_plugins] => 1
                            [edit_users] => 1
                            [edit_files] => 1
                            [manage_options] => 1
                            [moderate_comments] => 1
                            [manage_categories] => 1
                            [manage_links] => 1
                            [upload_files] => 1
                            [import] => 1
                            [unfiltered_html] => 1
                            [edit_posts] => 1
                            [edit_others_posts] => 1
                            [edit_published_posts] => 1
                            [publish_posts] => 1
                            [edit_pages] => 1
                            [read] => 1
                            [level_10] => 1
                            [level_9] => 1
                            [level_8] => 1
                            [level_7] => 1
                            [level_6] => 1
                            [level_5] => 1
                            [level_4] => 1
                            [level_3] => 1
                            [level_2] => 1
                            [level_1] => 1
                            [level_0] => 1
                            [edit_others_pages] => 1
                            [edit_published_pages] => 1
                            [publish_pages] => 1
                            [delete_pages] => 1
                            [delete_others_pages] => 1
                            [delete_published_pages] => 1
                            [delete_posts] => 1
                            [delete_others_posts] => 1
                            [delete_published_posts] => 1
                            [delete_private_posts] => 1
                            [edit_private_posts] => 1
                            [read_private_posts] => 1
                            [delete_private_pages] => 1
                            [edit_private_pages] => 1
                            [read_private_pages] => 1
                            [delete_users] => 1
                            [create_users] => 1
                            [unfiltered_upload] => 1
                            [edit_dashboard] => 1
                            [update_plugins] => 1
                            [delete_plugins] => 1
                            [install_plugins] => 1
                            [update_themes] => 1
                            [install_themes] => 1
                            [update_core] => 1
                            [list_users] => 1
                            [remove_users] => 1
                            [add_users] => 1
                            [promote_users] => 1
                            [edit_theme_options] => 1
                            [delete_themes] => 1
                            [export] => 1
                            [administrator] => 1
                        )

                    [filter] => 
                    [user_login] => admin
                    [user_nicename] => admin
                    [user_email] => goranefbl@gmail.com
                    [user_url] => 
                    [user_registered] => 2014-01-29 10:57:09
                    [user_activation_key] => 
                    [user_status] => 0
                    [display_name] => admin
                    [wlm_feed_url] => http://p...content-available-to-author-only...n.com/excel/feed/?wpmfeedkey=1;2e7e48ca65d94e5f0ec1baae46e4972c
                    [wpm_login_date] => 1392155735
                    [wpm_login_ip] => 62.68.119.252
                )

            [Sequential] => 
            [Levels] => Array
                (
                    [1391447567] => stdClass Object
                        (
                            [Level_ID] => 1391447566
                            [Name] => Team Membership
                            [Cancelled] => 
                            [CancelDate] => 
                            [Pending] => 
                            [UnConfirmed] => 
                            [Expired] => 
                            [ExpiryDate] => 1393866766
                            [SequentialCancelled] => 
                            [Active] => 1
                            [Status] => Array
                                (
                                    [0] => Active
                                )

                            [Timestamp] => 1391447566
                            [TxnID] => WL-1-1391447566
                        )

                )

            [PayPerPosts] => Array
                (
                )

        )

)
</FONT></PRE>

<PRE><FONT COLOR=RED>Array
(
    [1391447567] => stdClass Object
        (
            [Level_ID] => 1391447566
            [Name] => Team Membership
            [Cancelled] => 
            [CancelDate] => 
            [Pending] => 
            [UnConfirmed] => 
            [Expired] => 
            [ExpiryDate] => 1393866766
            [SequentialCancelled] => 
            [Active] => 1
            [Status] => Array
                (
                    [0] => Active
                )

            [Timestamp] => 1391447566
            [TxnID] => WL-1-1391447566
        )

)
</FONT></PRE>
<BR><FONT COLOR=GREEN>KEY:</FONT> <FONT COLOR=BLUE>1391447567</FONT>
<BR><FONT COLOR=GREEN>VAL:</FONT> <FONT COLOR=BLUE><PRE>stdClass Object
(
    [Level_ID] => 1391447566
    [Name] => Team Membership
    [Cancelled] => 
    [CancelDate] => 
    [Pending] => 
    [UnConfirmed] => 
    [Expired] => 
    [ExpiryDate] => 1393866766
    [SequentialCancelled] => 
    [Active] => 1
    [Status] => Array
        (
            [0] => Active
        )

    [Timestamp] => 1391447566
    [TxnID] => WL-1-1391447566
)
</PRE></FONT>
<BR><FONT COLOR=GREEN>NAME:</FONT> <FONT COLOR=BLUE>Team Membership</FONT>

<BR><BR>Final Captured Level Name: Team Membership