Eli
Eli

Reputation: 4359

echo an array in __toString method

I'm having a difficult time returning an array in my __toString method.

Here is my code.

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

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

I have been playing around with it but no mater what I do I still get this error

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

Is it not possible to return an array?

this is the array i am trying to implode

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
                                )

                        )

                )

        )

)

Upvotes: 0

Views: 1481

Answers (2)

Baba
Baba

Reputation: 95101

You can not use implode on a multi dimensional array

Example

$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" );

Output

  Notice: Array to string conversion in xxxx

Demo 1

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

Output

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

Demo 2

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

Output

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

Function Used

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 );
}

Upvotes: 1

John Conde
John Conde

Reputation: 219804

Why are you echoing the return value and then returning a PHP construct?

Just return your variable:

return $version;

Update

I am pretty sure you can't implode a multi-dimensional array. You'll need to write a custom callback and call array_walk for that to work.

Upvotes: 2

Related Questions