数组到字符串转换

时间:2017-07-04 22:39:01

标签: php laravel

这是我的代码:

$menu .= $this->$item['type']($item);

$ item [' type']是一个字符串 - ' single'它使用方法single()。然而浏览器向我显示了这个错误:

Array to string conversion

我不知道为什么,因为single返回一个字符串,$ item [' type']也是一个字符串。我甚至试过检查这个:

var_dump($this->$item['type']([]));

它仍会返回相同的错误。你知道为什么吗?

2 个答案:

答案 0 :(得分:1)

我相信这会起作用尝试:

$method = $item['type'];
var_dump($this->$method($item));

答案 1 :(得分:1)

在将数组元素用作对象的方法名称之前,需要将数组元素中的字符串包装在大括号中:

if user.is_a?(User) && user.is_mentor

见这里:http://php.net/manual/en/language.types.object.php