<?php

// ORIGINAL ARRAY STRING FROM POST
$string_of_array = '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
                (
                    [1391447566] => 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
                (
                )

        )

)';



// CONVERT THE STRING TO AN ARRAY
$response = print_r_reverse($string_of_array);



// RECREATE THE OBJECT PART OF THE ARRAY
$object = new stdClass();
$object->Level_ID = 1391447566;
$object->Name = 'Team Membership';
$object->Cancelled = '';
$object->CancelDate = ''; 
$object->Pending = ''; 
$object->UnConfirmed = ''; 
$object->Expired = ''; 
$object->ExpiryDate = 1393866766;
$object->SequentialCancelled = '';
$object->Active = 1;
$object->Status = array('Active');
$object->Timestamp = 1391447566;
$object->TxnID = 'WL-1-1391447566';



// ADD THE OBJECT TO THE ARRAY
$response[0]['Levels'][] = $object;



// UNSET THE ORIGINAL MOCK OBJECT SINCE IT'S NOT A REAL OBJECT
unset($response[0]['Levels']['1391447566']);


// PRINT THE FULL ARRAY TO MAKE SURE WE ARE ON TRACK
print "\n\n<PRE><FONT COLOR=ORANGE>"; print_r($response); print "</FONT></PRE>";


// LOOK AT THE LEVELS PART OF THE ARRAY SPECIFICALLY
print "\n\n<PRE><FONT COLOR=RED>"; print_r($response[0]['Levels']); print "</FONT></PRE>";


// LOOP THROUGH EACH LEVEL AND PULL OUT THE NAME
foreach ($response[0]['Levels'] AS $level_key => $level_val) {


    // PRINT OUT DEBUG STUFF
    print "\n<BR><FONT COLOR=GREEN>KEY:</FONT> <FONT COLOR=BLUE>".$level_key."</FONT>";
    print "\n<BR><FONT COLOR=GREEN>VAL:</FONT> <FONT COLOR=BLUE><PRE>".print_r($level_val, true)."</PRE></FONT>";
    print "\n<BR><FONT COLOR=GREEN>NAME:</FONT> <FONT COLOR=BLUE>".$level_val->Name."</FONT>";


    // ASSIGN THE LEVEL NAME TO A VARIABLE
    $level_name = $level_val->Name;

}


// PRINT THE OUTPUT
echo "\n\n<BR><BR>Final Captured Level Name: ".$level_name;



// FUNCTION TO TURN THE 'print_r' VERSION OF THE ARRAY INTO AN ACTUAL ARRAY
function print_r_reverse($in) {
    $lines = explode("\n", trim($in));
    if (trim($lines[0]) != 'Array') {
        // bottomed out to something that isn't an array
        return $in;
    } else {
        // this is an array, lets parse it
        if (preg_match("/(\s{5,})\(/", $lines[1], $match)) {
            // this is a tested array/recursive call to this function
            // take a set of spaces off the beginning
            $spaces = $match[1];
            $spaces_length = strlen($spaces);
            $lines_total = count($lines);
            for ($i = 0; $i < $lines_total; $i++) {
                if (substr($lines[$i], 0, $spaces_length) == $spaces) {
                    $lines[$i] = substr($lines[$i], $spaces_length);
                }
            }
        }
        array_shift($lines); // Array
        array_shift($lines); // (
        array_pop($lines); // )
        $in = implode("\n", $lines);
        // make sure we only match stuff with 4 preceding spaces (stuff for this array and not a nested one)
        preg_match_all("/^\s{4}\[(.+?)\] \=\> /m", $in, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
        $pos = array();
        $previous_key = '';
        $in_length = strlen($in);
        // store the following in $pos:
        // array with key = key of the parsed array's item
        // value = array(start position in $in, $end position in $in)
        foreach ($matches as $match) {
            $key = $match[1][0];
            $start = $match[0][1] + strlen($match[0][0]);
            $pos[$key] = array($start, $in_length);
            if ($previous_key != '') $pos[$previous_key][1] = $match[0][1] - 1;
            $previous_key = $key;
        }
        $ret = array();
        foreach ($pos as $key => $where) {
            // recursively see if the parsed out value is an array too
            $ret[$key] = print_r_reverse(substr($in, $where[0], $where[1] - $where[0]));
        }
        return $ret;
    }
} 