我正在将一个标准 php 类引入 Laravel。
我遇到的问题是使用 array_walk_recursive() 调用可打印函数。
这是我班级的代码片段:
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("<", "<", str_replace(">", ">", $v)));
}
}
}
错误:
“array_walk_recursive() 期望参数 2 是有效的回调,未找到函数“printable”或函数名称无效”
所以看起来它只是找不到可打印的功能。我需要做哪些不同的事情?
您需要指定对象。在这种情况下,它将是
self
:
array_walk_recursive($response, 'self::printable');