有没有一种简单的方法来获取print_r或var_dump的输出并将其转换为数组?

I want to take the output of a var_dump or print_r and convert it to an array that mirrors what was in the original.

convert this:

stdClass Object
(
    [title] => Edison's Friends
    [status] => 1

    [field_deck_owner] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [uid] => 15
                        )

                )

        )

    [name] => Mack
)

to this:

$array =  array(
    'title' -> 'Edison's Friends',
    'status' -> '1',
    'field_deck_owner' = array (
        array(
            array (
                'uid'->15
                )
            )
        ),
    'name' = "Mack"

);

Are there scripts that do this? A javascript site ;-) Something?

Not a parser like you're asking for, but if you just want an array of the object's properties you can just cast it to an array directly.

Ummmm just set it equal..

If you are doing this:

var_dump($something);

SO DO:

$something_else = $something;

And viola! you now have a copy.


Update:

Actually you can try this: get_object_vars()

var_dump($something);

SO DO:

$something_else = get_object_vars($something);

There's no easy way nor built-in PHP function to do that. Use serialize().

Maybe you can change the code of any JSON class parser written in PHP for this sintaxis, seems similar.

If your object is really just a StdClass then cast it to an array

$array = (array)$object;

If the object has protected or private properties you might get undesirable results (null bytes in property names). In that case I would look at for public only properties

$array = get_object_vars($object)

i use this function :

function std_class_object_to_array($stdclassobject) 
{ 
    $_array = is_object($stdclassobject) ? get_object_vars($stdclassobject) :   $stdclassobject;
    foreach ($_array as $key => $value)  
    { 
        $value = (is_array($value) || is_object($value)) ? std_class_object_to_array($value) : $value; $array[$key] = $value;  
    } 
    return $array; 
}

You can use the code below to reverse a print_r of a stdClass or a Array.

You just have to use the reversePrint_r function, it expects the first parameter to be a split of the print_r output, the second parameter is the index of the first variable of the print_r output, note that you can't put directly the index because is a referenced parameter, and the third parameter indicates if is an array or a stdObject.

function reversePrint_r($stringObject, &$index, $isArray) {
        $result = ($isArray) ? array () : new stdClass ();
        $continue = true;
        $numberSpacesOriginal = ((preg_match_all ( "/ +/", $stringObject [$index], $matches )) ? strlen ( $matches [0] [0] ) : 0);
        while ( $continue ) {
            $line = $stringObject [$index];
            $numberSpaces = ((preg_match_all ( "/ +/", $line, $matches )) ? strlen ( $matches [0] [0] ) : 0);
            if ((preg_match ( "/Array$/", $line )) || (preg_match ( "/stdClass Object$/", $line ))) {
                $isNextArray = (preg_match ( "/Array$/", $line )) ? true : false;
                $var_name = getVariableNameFromLine ( $line );
                $index += 2;
                $parameter = getListFromStdClass ( $stringObject, $index, $isNextArray );
                addResult ( $result, $var_name, $parameter, $isArray );
                $index ++;
            } else {
                $var_name = getVariableNameFromLine ( $line );
                $parameter = getParameterFromLine ( $line );
                addResult ( $result, $var_name, $parameter, $isArray );
            }
            $index ++;
            $numberSpaces = ((preg_match_all ( "/ +/", $stringObject [$index], $matches )) ? strlen ( $matches [0] [0] ) : 0);
            if ($numberSpaces != $numberSpacesOriginal) {
                $continue = false;
            }
        }
        return $result;
    }

function addResult(&$result, $key, $value, $isArray) {
    if (! $isArray) {
        $result = ( object ) array_merge ( ( array ) $result, array (
                $key => $value 
        ) );
    } else {
        $result [$key] = $value;
    }
}

function getVariableNameFromLine($line) {
    $result = split ( preg_quote ( "=>" ), $line );
    $result = preg_replace ( "/( *\[|\] *)/", "", $result [0] );
    return $result;
}

function getParameterFromLine($line) {
    $result = preg_replace ( "/^.*=> /", "", $line );
    $result = preg_replace("/
/","",$result);
    return $result;
}