从对象到人类可读文本[关闭]

I am working on a small function which will convert objects to strings. It is easy if object has values as arrays. But I want this function to work even my object or array has some values as another object or array. I make it very coarse so experts please help this to make it tidy for everyone.

My function:

function makeString($array)
{
    $outval = "";  
    foreach($array as $key=>$value) {
        if (is_object($value)) {
            $arr = array();
            $arr = get_object_vars($value);

            foreach($arr as $key1=>$value1){
                if(is_array($value1)) { 
                    $outval .= "\t\t$key1
"; 
                    $outval .= makeString($value1);
                }  
                else {
                    $outval .= "\t$key1: $value1
";
                } 

            }
        }
        if(is_array($value)) { 
            $outval .= "\t$key
"; 
            $outval .= makeString($value);
        }  
        else {
            $outval .= "$key: $value
";
        }  
    } 
    return $outval;  
}  

This part is recurring inside my function:

...
    if (is_object($value)) {
        $arr = array();
        $arr = get_object_vars($value);

        foreach($arr as $key1=>$value1){
            if(is_array($value1)) { 
                $outval .= "\t\t$key1
"; 
                $outval .= makeString($value1);
            }  
            else {
                $outval .= "\t$key1: $value1
";
            } 

        }
    }
...

What if $value1 here is also an object? What should I do to make this function work even if my object has many other objects or arrays inside main object.

Your function suffers from a flaw which comes from your one assumption: That the function will be passed an array. However, as you're finding out, this is not guaranteed.

So, a better way to design such a function is to not assume anything about the input, and restructure your function like this:

function makeString( $value) {
    if( is_object( $value)) { }
    else if( is_array( $value)) { }
    else if( is_string( $value) || is_numeric( $value)) { }
    else { } // Resource, etc.
}