0

我很难在我的__toString方法中返回一个数组。

这是我的代码。

public function __toString()
{
    $this->_libraries();
    $this->_sysConfig();

    //print_r($this->version);
    $version = implode("\n", $this->version);
    echo $version;
    return 'echo';//$version;
}

我一直在玩它,但无论我做什么,我仍然会收到此错误

Notice: Array to string conversion in (...)

不能返回数组吗?

这是我试图内爆的数组

Array
(
    [0] => Array
        (
            [version] => Array
                (
                    [item] => Array
                        (
                            [0] => Array
                                (
                                    [name] => cms
                                    [version] => 1.0
                                )

                            [1] => Array
                                (
                                    [name] => TinyMCE Jquery Package
                                    [version] => 3.5
                                )

                            [2] => Array
                                (
                                    [name] => Jquery
                                    [version] => 1.7.2
                                )

                        )

                )

        )

)
4

2 回答 2

2

为什么要回显返回值然后返回 PHP 构造?

只需返回您的变量:

return $version;

更新

我很确定你不能内爆多维数组。您需要编写一个自定义回调并调用 array_walk 以使其工作。

于 2012-05-04T16:38:55.033 回答
1

您不能implode在多维数组上使用

例子

$version = Array (
        0 => Array (
                'version' => Array (
                        'item' => Array (
                                0 => Array (
                                        'name' => 'cms',
                                        'version' => '1.0' 
                                ),

                                1 => Array (
                                        'name' => 'TinyMCE Jquery Package',
                                        'version' => '3.5' 
                                ),

                                2 => Array (
                                        'name' => 'Jquery',
                                        'version' => '1.7.2' 
                                ) 
                        ) 
                )

        )

);

$version = implode ( $version, "\n" );

输出

  Notice: Array to string conversion in xxxx

演示 1

 $version = multi_implode ( $version, "," );

输出

 cms1.0 , TinyMCE Jquery Package3.5 , Jquery1.7.2

演示 2

 $version = multi_implode ( $version, " , " ," = " );

输出

 cms = 1.0 , TinyMCE Jquery Package = 3.5 , Jquery = 1.7.2

使用的功能

function multi_implode($pieces, $glue, $glue2 = null) {
    foreach ( $pieces as $piece ) {
        if (is_array ( $piece )) {
            $retVal [] = multi_implode ( $piece, $glue, $glue2 );
        } else {
            if ($glue2 == null) {
                $retVal [] = $piece;
            } else {
                $retVal [] = implode ( $glue2, $pieces );
                break;
            }
        }
    }
    return implode ( $glue, $retVal );
}
于 2012-05-04T16:53:19.547 回答