2

I'm bringing a standard php class into Laravel.

The problem I'm having is calling the printable function using array_walk_recursive().

This the snippet of code from my class:

public static function print_r($response)
{
    // Format response (for testing)

    if (is_object($response)) $response = (array)$response;
    if (!is_array($response) && $response) $response = json_decode($response, true);
    if (is_array($response))
    {
        array_walk_recursive($response, "printable");
        echo "<pre>" . print_r($response, true) . "</pre>";
    }
}

private static function printable(&$v, $k)
{
    // Format response (for testing)

    if (!is_array($v))
    {
        if (is_bool($v))
        {
            if ($v) $v = "true"; else $v = "false";
        }
        else if (is_null($v))
        {
            $v = "null";
        }
        else
        {
            $v = trim(str_replace("<", "&lt;", str_replace(">", "&gt;", $v)));
        }
    }
}

The error:

"array_walk_recursive() expects parameter 2 to be a valid callback, function 'printable' not found or invalid function name"

So it seems like it's just not finding the printable function. What do I need to do differently?

1

1 Answer 1

2

You need to specify the object. In this case it will be self:

array_walk_recursive($response, 'self::printable');
Sign up to request clarification or add additional context in comments.

3 Comments

I'm afraid that gives the error "Undefined variable: this"
Oh yeah, you're using static methods. Please try updated code.
Thank you, It works for me. private function addSpan(&$value, $key) { $value = "<span>{$value}</span>"; } array_walk($replace, 'self::addSpan');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.